mt9v032.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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/clk.h>
  15. #include <linux/delay.h>
  16. #include <linux/i2c.h>
  17. #include <linux/log2.h>
  18. #include <linux/mutex.h>
  19. #include <linux/slab.h>
  20. #include <linux/videodev2.h>
  21. #include <linux/v4l2-mediabus.h>
  22. #include <linux/module.h>
  23. #include <media/mt9v032.h>
  24. #include <media/v4l2-ctrls.h>
  25. #include <media/v4l2-device.h>
  26. #include <media/v4l2-subdev.h>
  27. /* The first four rows are black rows. The active area spans 753x481 pixels. */
  28. #define MT9V032_PIXEL_ARRAY_HEIGHT 485
  29. #define MT9V032_PIXEL_ARRAY_WIDTH 753
  30. #define MT9V032_SYSCLK_FREQ_DEF 26600000
  31. #define MT9V032_CHIP_VERSION 0x00
  32. #define MT9V032_CHIP_ID_REV1 0x1311
  33. #define MT9V032_CHIP_ID_REV3 0x1313
  34. #define MT9V034_CHIP_ID_REV1 0X1324
  35. #define MT9V032_COLUMN_START 0x01
  36. #define MT9V032_COLUMN_START_MIN 1
  37. #define MT9V032_COLUMN_START_DEF 1
  38. #define MT9V032_COLUMN_START_MAX 752
  39. #define MT9V032_ROW_START 0x02
  40. #define MT9V032_ROW_START_MIN 4
  41. #define MT9V032_ROW_START_DEF 5
  42. #define MT9V032_ROW_START_MAX 482
  43. #define MT9V032_WINDOW_HEIGHT 0x03
  44. #define MT9V032_WINDOW_HEIGHT_MIN 1
  45. #define MT9V032_WINDOW_HEIGHT_DEF 480
  46. #define MT9V032_WINDOW_HEIGHT_MAX 480
  47. #define MT9V032_WINDOW_WIDTH 0x04
  48. #define MT9V032_WINDOW_WIDTH_MIN 1
  49. #define MT9V032_WINDOW_WIDTH_DEF 752
  50. #define MT9V032_WINDOW_WIDTH_MAX 752
  51. #define MT9V032_HORIZONTAL_BLANKING 0x05
  52. #define MT9V032_HORIZONTAL_BLANKING_MIN 43
  53. #define MT9V034_HORIZONTAL_BLANKING_MIN 61
  54. #define MT9V032_HORIZONTAL_BLANKING_DEF 94
  55. #define MT9V032_HORIZONTAL_BLANKING_MAX 1023
  56. #define MT9V032_VERTICAL_BLANKING 0x06
  57. #define MT9V032_VERTICAL_BLANKING_MIN 4
  58. #define MT9V034_VERTICAL_BLANKING_MIN 2
  59. #define MT9V032_VERTICAL_BLANKING_DEF 45
  60. #define MT9V032_VERTICAL_BLANKING_MAX 3000
  61. #define MT9V034_VERTICAL_BLANKING_MAX 32288
  62. #define MT9V032_CHIP_CONTROL 0x07
  63. #define MT9V032_CHIP_CONTROL_MASTER_MODE (1 << 3)
  64. #define MT9V032_CHIP_CONTROL_DOUT_ENABLE (1 << 7)
  65. #define MT9V032_CHIP_CONTROL_SEQUENTIAL (1 << 8)
  66. #define MT9V032_SHUTTER_WIDTH1 0x08
  67. #define MT9V032_SHUTTER_WIDTH2 0x09
  68. #define MT9V032_SHUTTER_WIDTH_CONTROL 0x0a
  69. #define MT9V032_TOTAL_SHUTTER_WIDTH 0x0b
  70. #define MT9V032_TOTAL_SHUTTER_WIDTH_MIN 1
  71. #define MT9V034_TOTAL_SHUTTER_WIDTH_MIN 0
  72. #define MT9V032_TOTAL_SHUTTER_WIDTH_DEF 480
  73. #define MT9V032_TOTAL_SHUTTER_WIDTH_MAX 32767
  74. #define MT9V034_TOTAL_SHUTTER_WIDTH_MAX 32765
  75. #define MT9V032_RESET 0x0c
  76. #define MT9V032_READ_MODE 0x0d
  77. #define MT9V032_READ_MODE_ROW_BIN_MASK (3 << 0)
  78. #define MT9V032_READ_MODE_ROW_BIN_SHIFT 0
  79. #define MT9V032_READ_MODE_COLUMN_BIN_MASK (3 << 2)
  80. #define MT9V032_READ_MODE_COLUMN_BIN_SHIFT 2
  81. #define MT9V032_READ_MODE_ROW_FLIP (1 << 4)
  82. #define MT9V032_READ_MODE_COLUMN_FLIP (1 << 5)
  83. #define MT9V032_READ_MODE_DARK_COLUMNS (1 << 6)
  84. #define MT9V032_READ_MODE_DARK_ROWS (1 << 7)
  85. #define MT9V032_PIXEL_OPERATION_MODE 0x0f
  86. #define MT9V034_PIXEL_OPERATION_MODE_HDR (1 << 0)
  87. #define MT9V034_PIXEL_OPERATION_MODE_COLOR (1 << 1)
  88. #define MT9V032_PIXEL_OPERATION_MODE_COLOR (1 << 2)
  89. #define MT9V032_PIXEL_OPERATION_MODE_HDR (1 << 6)
  90. #define MT9V032_ANALOG_GAIN 0x35
  91. #define MT9V032_ANALOG_GAIN_MIN 16
  92. #define MT9V032_ANALOG_GAIN_DEF 16
  93. #define MT9V032_ANALOG_GAIN_MAX 64
  94. #define MT9V032_MAX_ANALOG_GAIN 0x36
  95. #define MT9V032_MAX_ANALOG_GAIN_MAX 127
  96. #define MT9V032_FRAME_DARK_AVERAGE 0x42
  97. #define MT9V032_DARK_AVG_THRESH 0x46
  98. #define MT9V032_DARK_AVG_LOW_THRESH_MASK (255 << 0)
  99. #define MT9V032_DARK_AVG_LOW_THRESH_SHIFT 0
  100. #define MT9V032_DARK_AVG_HIGH_THRESH_MASK (255 << 8)
  101. #define MT9V032_DARK_AVG_HIGH_THRESH_SHIFT 8
  102. #define MT9V032_ROW_NOISE_CORR_CONTROL 0x70
  103. #define MT9V034_ROW_NOISE_CORR_ENABLE (1 << 0)
  104. #define MT9V034_ROW_NOISE_CORR_USE_BLK_AVG (1 << 1)
  105. #define MT9V032_ROW_NOISE_CORR_ENABLE (1 << 5)
  106. #define MT9V032_ROW_NOISE_CORR_USE_BLK_AVG (1 << 7)
  107. #define MT9V032_PIXEL_CLOCK 0x74
  108. #define MT9V034_PIXEL_CLOCK 0x72
  109. #define MT9V032_PIXEL_CLOCK_INV_LINE (1 << 0)
  110. #define MT9V032_PIXEL_CLOCK_INV_FRAME (1 << 1)
  111. #define MT9V032_PIXEL_CLOCK_XOR_LINE (1 << 2)
  112. #define MT9V032_PIXEL_CLOCK_CONT_LINE (1 << 3)
  113. #define MT9V032_PIXEL_CLOCK_INV_PXL_CLK (1 << 4)
  114. #define MT9V032_TEST_PATTERN 0x7f
  115. #define MT9V032_TEST_PATTERN_DATA_MASK (1023 << 0)
  116. #define MT9V032_TEST_PATTERN_DATA_SHIFT 0
  117. #define MT9V032_TEST_PATTERN_USE_DATA (1 << 10)
  118. #define MT9V032_TEST_PATTERN_GRAY_MASK (3 << 11)
  119. #define MT9V032_TEST_PATTERN_GRAY_NONE (0 << 11)
  120. #define MT9V032_TEST_PATTERN_GRAY_VERTICAL (1 << 11)
  121. #define MT9V032_TEST_PATTERN_GRAY_HORIZONTAL (2 << 11)
  122. #define MT9V032_TEST_PATTERN_GRAY_DIAGONAL (3 << 11)
  123. #define MT9V032_TEST_PATTERN_ENABLE (1 << 13)
  124. #define MT9V032_TEST_PATTERN_FLIP (1 << 14)
  125. #define MT9V032_AEC_AGC_ENABLE 0xaf
  126. #define MT9V032_AEC_ENABLE (1 << 0)
  127. #define MT9V032_AGC_ENABLE (1 << 1)
  128. #define MT9V032_THERMAL_INFO 0xc1
  129. enum mt9v032_model {
  130. MT9V032_MODEL_V032_COLOR,
  131. MT9V032_MODEL_V032_MONO,
  132. MT9V032_MODEL_V034_COLOR,
  133. MT9V032_MODEL_V034_MONO,
  134. };
  135. struct mt9v032_model_version {
  136. unsigned int version;
  137. const char *name;
  138. };
  139. struct mt9v032_model_data {
  140. unsigned int min_row_time;
  141. unsigned int min_hblank;
  142. unsigned int min_vblank;
  143. unsigned int max_vblank;
  144. unsigned int min_shutter;
  145. unsigned int max_shutter;
  146. unsigned int pclk_reg;
  147. };
  148. struct mt9v032_model_info {
  149. const struct mt9v032_model_data *data;
  150. bool color;
  151. };
  152. static const struct mt9v032_model_version mt9v032_versions[] = {
  153. { MT9V032_CHIP_ID_REV1, "MT9V032 rev1/2" },
  154. { MT9V032_CHIP_ID_REV3, "MT9V032 rev3" },
  155. { MT9V034_CHIP_ID_REV1, "MT9V034 rev1" },
  156. };
  157. static const struct mt9v032_model_data mt9v032_model_data[] = {
  158. {
  159. /* MT9V032 revisions 1/2/3 */
  160. .min_row_time = 660,
  161. .min_hblank = MT9V032_HORIZONTAL_BLANKING_MIN,
  162. .min_vblank = MT9V032_VERTICAL_BLANKING_MIN,
  163. .max_vblank = MT9V032_VERTICAL_BLANKING_MAX,
  164. .min_shutter = MT9V032_TOTAL_SHUTTER_WIDTH_MIN,
  165. .max_shutter = MT9V032_TOTAL_SHUTTER_WIDTH_MAX,
  166. .pclk_reg = MT9V032_PIXEL_CLOCK,
  167. }, {
  168. /* MT9V034 */
  169. .min_row_time = 690,
  170. .min_hblank = MT9V034_HORIZONTAL_BLANKING_MIN,
  171. .min_vblank = MT9V034_VERTICAL_BLANKING_MIN,
  172. .max_vblank = MT9V034_VERTICAL_BLANKING_MAX,
  173. .min_shutter = MT9V034_TOTAL_SHUTTER_WIDTH_MIN,
  174. .max_shutter = MT9V034_TOTAL_SHUTTER_WIDTH_MAX,
  175. .pclk_reg = MT9V034_PIXEL_CLOCK,
  176. },
  177. };
  178. static const struct mt9v032_model_info mt9v032_models[] = {
  179. [MT9V032_MODEL_V032_COLOR] = {
  180. .data = &mt9v032_model_data[0],
  181. .color = true,
  182. },
  183. [MT9V032_MODEL_V032_MONO] = {
  184. .data = &mt9v032_model_data[0],
  185. .color = false,
  186. },
  187. [MT9V032_MODEL_V034_COLOR] = {
  188. .data = &mt9v032_model_data[1],
  189. .color = true,
  190. },
  191. [MT9V032_MODEL_V034_MONO] = {
  192. .data = &mt9v032_model_data[1],
  193. .color = false,
  194. },
  195. };
  196. struct mt9v032 {
  197. struct v4l2_subdev subdev;
  198. struct media_pad pad;
  199. struct v4l2_mbus_framefmt format;
  200. struct v4l2_rect crop;
  201. unsigned int hratio;
  202. unsigned int vratio;
  203. struct v4l2_ctrl_handler ctrls;
  204. struct {
  205. struct v4l2_ctrl *link_freq;
  206. struct v4l2_ctrl *pixel_rate;
  207. };
  208. struct mutex power_lock;
  209. int power_count;
  210. struct clk *clk;
  211. struct mt9v032_platform_data *pdata;
  212. const struct mt9v032_model_info *model;
  213. const struct mt9v032_model_version *version;
  214. u32 sysclk;
  215. u16 chip_control;
  216. u16 aec_agc;
  217. u16 hblank;
  218. struct {
  219. struct v4l2_ctrl *test_pattern;
  220. struct v4l2_ctrl *test_pattern_color;
  221. };
  222. };
  223. static struct mt9v032 *to_mt9v032(struct v4l2_subdev *sd)
  224. {
  225. return container_of(sd, struct mt9v032, subdev);
  226. }
  227. static int mt9v032_read(struct i2c_client *client, const u8 reg)
  228. {
  229. s32 data = i2c_smbus_read_word_swapped(client, reg);
  230. dev_dbg(&client->dev, "%s: read 0x%04x from 0x%02x\n", __func__,
  231. data, reg);
  232. return data;
  233. }
  234. static int mt9v032_write(struct i2c_client *client, const u8 reg,
  235. const u16 data)
  236. {
  237. dev_dbg(&client->dev, "%s: writing 0x%04x to 0x%02x\n", __func__,
  238. data, reg);
  239. return i2c_smbus_write_word_swapped(client, reg, data);
  240. }
  241. static int mt9v032_set_chip_control(struct mt9v032 *mt9v032, u16 clear, u16 set)
  242. {
  243. struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
  244. u16 value = (mt9v032->chip_control & ~clear) | set;
  245. int ret;
  246. ret = mt9v032_write(client, MT9V032_CHIP_CONTROL, value);
  247. if (ret < 0)
  248. return ret;
  249. mt9v032->chip_control = value;
  250. return 0;
  251. }
  252. static int
  253. mt9v032_update_aec_agc(struct mt9v032 *mt9v032, u16 which, int enable)
  254. {
  255. struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
  256. u16 value = mt9v032->aec_agc;
  257. int ret;
  258. if (enable)
  259. value |= which;
  260. else
  261. value &= ~which;
  262. ret = mt9v032_write(client, MT9V032_AEC_AGC_ENABLE, value);
  263. if (ret < 0)
  264. return ret;
  265. mt9v032->aec_agc = value;
  266. return 0;
  267. }
  268. static int
  269. mt9v032_update_hblank(struct mt9v032 *mt9v032)
  270. {
  271. struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
  272. struct v4l2_rect *crop = &mt9v032->crop;
  273. unsigned int min_hblank = mt9v032->model->data->min_hblank;
  274. unsigned int hblank;
  275. if (mt9v032->version->version == MT9V034_CHIP_ID_REV1)
  276. min_hblank += (mt9v032->hratio - 1) * 10;
  277. min_hblank = max_t(unsigned int, (int)mt9v032->model->data->min_row_time - crop->width,
  278. (int)min_hblank);
  279. hblank = max_t(unsigned int, mt9v032->hblank, min_hblank);
  280. return mt9v032_write(client, MT9V032_HORIZONTAL_BLANKING, hblank);
  281. }
  282. static int mt9v032_power_on(struct mt9v032 *mt9v032)
  283. {
  284. struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
  285. int ret;
  286. ret = clk_set_rate(mt9v032->clk, mt9v032->sysclk);
  287. if (ret < 0)
  288. return ret;
  289. ret = clk_prepare_enable(mt9v032->clk);
  290. if (ret)
  291. return ret;
  292. udelay(1);
  293. /* Reset the chip and stop data read out */
  294. ret = mt9v032_write(client, MT9V032_RESET, 1);
  295. if (ret < 0)
  296. return ret;
  297. ret = mt9v032_write(client, MT9V032_RESET, 0);
  298. if (ret < 0)
  299. return ret;
  300. return mt9v032_write(client, MT9V032_CHIP_CONTROL, 0);
  301. }
  302. static void mt9v032_power_off(struct mt9v032 *mt9v032)
  303. {
  304. clk_disable_unprepare(mt9v032->clk);
  305. }
  306. static int __mt9v032_set_power(struct mt9v032 *mt9v032, bool on)
  307. {
  308. struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
  309. int ret;
  310. if (!on) {
  311. mt9v032_power_off(mt9v032);
  312. return 0;
  313. }
  314. ret = mt9v032_power_on(mt9v032);
  315. if (ret < 0)
  316. return ret;
  317. /* Configure the pixel clock polarity */
  318. if (mt9v032->pdata && mt9v032->pdata->clk_pol) {
  319. ret = mt9v032_write(client, mt9v032->model->data->pclk_reg,
  320. MT9V032_PIXEL_CLOCK_INV_PXL_CLK);
  321. if (ret < 0)
  322. return ret;
  323. }
  324. /* Disable the noise correction algorithm and restore the controls. */
  325. ret = mt9v032_write(client, MT9V032_ROW_NOISE_CORR_CONTROL, 0);
  326. if (ret < 0)
  327. return ret;
  328. return v4l2_ctrl_handler_setup(&mt9v032->ctrls);
  329. }
  330. /* -----------------------------------------------------------------------------
  331. * V4L2 subdev video operations
  332. */
  333. static struct v4l2_mbus_framefmt *
  334. __mt9v032_get_pad_format(struct mt9v032 *mt9v032, struct v4l2_subdev_fh *fh,
  335. unsigned int pad, enum v4l2_subdev_format_whence which)
  336. {
  337. switch (which) {
  338. case V4L2_SUBDEV_FORMAT_TRY:
  339. return v4l2_subdev_get_try_format(fh, pad);
  340. case V4L2_SUBDEV_FORMAT_ACTIVE:
  341. return &mt9v032->format;
  342. default:
  343. return NULL;
  344. }
  345. }
  346. static struct v4l2_rect *
  347. __mt9v032_get_pad_crop(struct mt9v032 *mt9v032, struct v4l2_subdev_fh *fh,
  348. unsigned int pad, enum v4l2_subdev_format_whence which)
  349. {
  350. switch (which) {
  351. case V4L2_SUBDEV_FORMAT_TRY:
  352. return v4l2_subdev_get_try_crop(fh, pad);
  353. case V4L2_SUBDEV_FORMAT_ACTIVE:
  354. return &mt9v032->crop;
  355. default:
  356. return NULL;
  357. }
  358. }
  359. static int mt9v032_s_stream(struct v4l2_subdev *subdev, int enable)
  360. {
  361. const u16 mode = MT9V032_CHIP_CONTROL_MASTER_MODE
  362. | MT9V032_CHIP_CONTROL_DOUT_ENABLE
  363. | MT9V032_CHIP_CONTROL_SEQUENTIAL;
  364. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  365. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  366. struct v4l2_rect *crop = &mt9v032->crop;
  367. unsigned int hbin;
  368. unsigned int vbin;
  369. int ret;
  370. if (!enable)
  371. return mt9v032_set_chip_control(mt9v032, mode, 0);
  372. /* Configure the window size and row/column bin */
  373. hbin = fls(mt9v032->hratio) - 1;
  374. vbin = fls(mt9v032->vratio) - 1;
  375. ret = mt9v032_write(client, MT9V032_READ_MODE,
  376. hbin << MT9V032_READ_MODE_COLUMN_BIN_SHIFT |
  377. vbin << MT9V032_READ_MODE_ROW_BIN_SHIFT);
  378. if (ret < 0)
  379. return ret;
  380. ret = mt9v032_write(client, MT9V032_COLUMN_START, crop->left);
  381. if (ret < 0)
  382. return ret;
  383. ret = mt9v032_write(client, MT9V032_ROW_START, crop->top);
  384. if (ret < 0)
  385. return ret;
  386. ret = mt9v032_write(client, MT9V032_WINDOW_WIDTH, crop->width);
  387. if (ret < 0)
  388. return ret;
  389. ret = mt9v032_write(client, MT9V032_WINDOW_HEIGHT, crop->height);
  390. if (ret < 0)
  391. return ret;
  392. ret = mt9v032_update_hblank(mt9v032);
  393. if (ret < 0)
  394. return ret;
  395. /* Switch to master "normal" mode */
  396. return mt9v032_set_chip_control(mt9v032, 0, mode);
  397. }
  398. static int mt9v032_enum_mbus_code(struct v4l2_subdev *subdev,
  399. struct v4l2_subdev_fh *fh,
  400. struct v4l2_subdev_mbus_code_enum *code)
  401. {
  402. if (code->index > 0)
  403. return -EINVAL;
  404. code->code = V4L2_MBUS_FMT_SGRBG10_1X10;
  405. return 0;
  406. }
  407. static int mt9v032_enum_frame_size(struct v4l2_subdev *subdev,
  408. struct v4l2_subdev_fh *fh,
  409. struct v4l2_subdev_frame_size_enum *fse)
  410. {
  411. if (fse->index >= 3 || fse->code != V4L2_MBUS_FMT_SGRBG10_1X10)
  412. return -EINVAL;
  413. fse->min_width = MT9V032_WINDOW_WIDTH_DEF / (1 << fse->index);
  414. fse->max_width = fse->min_width;
  415. fse->min_height = MT9V032_WINDOW_HEIGHT_DEF / (1 << fse->index);
  416. fse->max_height = fse->min_height;
  417. return 0;
  418. }
  419. static int mt9v032_get_format(struct v4l2_subdev *subdev,
  420. struct v4l2_subdev_fh *fh,
  421. struct v4l2_subdev_format *format)
  422. {
  423. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  424. format->format = *__mt9v032_get_pad_format(mt9v032, fh, format->pad,
  425. format->which);
  426. return 0;
  427. }
  428. static void mt9v032_configure_pixel_rate(struct mt9v032 *mt9v032)
  429. {
  430. struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
  431. int ret;
  432. ret = v4l2_ctrl_s_ctrl_int64(mt9v032->pixel_rate,
  433. mt9v032->sysclk / mt9v032->hratio);
  434. if (ret < 0)
  435. dev_warn(&client->dev, "failed to set pixel rate (%d)\n", ret);
  436. }
  437. static unsigned int mt9v032_calc_ratio(unsigned int input, unsigned int output)
  438. {
  439. /* Compute the power-of-two binning factor closest to the input size to
  440. * output size ratio. Given that the output size is bounded by input/4
  441. * and input, a generic implementation would be an ineffective luxury.
  442. */
  443. if (output * 3 > input * 2)
  444. return 1;
  445. if (output * 3 > input)
  446. return 2;
  447. return 4;
  448. }
  449. static int mt9v032_set_format(struct v4l2_subdev *subdev,
  450. struct v4l2_subdev_fh *fh,
  451. struct v4l2_subdev_format *format)
  452. {
  453. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  454. struct v4l2_mbus_framefmt *__format;
  455. struct v4l2_rect *__crop;
  456. unsigned int width;
  457. unsigned int height;
  458. unsigned int hratio;
  459. unsigned int vratio;
  460. __crop = __mt9v032_get_pad_crop(mt9v032, fh, format->pad,
  461. format->which);
  462. /* Clamp the width and height to avoid dividing by zero. */
  463. width = clamp(ALIGN(format->format.width, 2),
  464. max_t(unsigned int, __crop->width / 4,
  465. MT9V032_WINDOW_WIDTH_MIN),
  466. __crop->width);
  467. height = clamp(ALIGN(format->format.height, 2),
  468. max_t(unsigned int, __crop->height / 4,
  469. MT9V032_WINDOW_HEIGHT_MIN),
  470. __crop->height);
  471. hratio = mt9v032_calc_ratio(__crop->width, width);
  472. vratio = mt9v032_calc_ratio(__crop->height, height);
  473. __format = __mt9v032_get_pad_format(mt9v032, fh, format->pad,
  474. format->which);
  475. __format->width = __crop->width / hratio;
  476. __format->height = __crop->height / vratio;
  477. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  478. mt9v032->hratio = hratio;
  479. mt9v032->vratio = vratio;
  480. mt9v032_configure_pixel_rate(mt9v032);
  481. }
  482. format->format = *__format;
  483. return 0;
  484. }
  485. static int mt9v032_get_crop(struct v4l2_subdev *subdev,
  486. struct v4l2_subdev_fh *fh,
  487. struct v4l2_subdev_crop *crop)
  488. {
  489. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  490. crop->rect = *__mt9v032_get_pad_crop(mt9v032, fh, crop->pad,
  491. crop->which);
  492. return 0;
  493. }
  494. static int mt9v032_set_crop(struct v4l2_subdev *subdev,
  495. struct v4l2_subdev_fh *fh,
  496. struct v4l2_subdev_crop *crop)
  497. {
  498. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  499. struct v4l2_mbus_framefmt *__format;
  500. struct v4l2_rect *__crop;
  501. struct v4l2_rect rect;
  502. /* Clamp the crop rectangle boundaries and align them to a non multiple
  503. * of 2 pixels to ensure a GRBG Bayer pattern.
  504. */
  505. rect.left = clamp(ALIGN(crop->rect.left + 1, 2) - 1,
  506. MT9V032_COLUMN_START_MIN,
  507. MT9V032_COLUMN_START_MAX);
  508. rect.top = clamp(ALIGN(crop->rect.top + 1, 2) - 1,
  509. MT9V032_ROW_START_MIN,
  510. MT9V032_ROW_START_MAX);
  511. rect.width = clamp_t(unsigned int, ALIGN(crop->rect.width, 2),
  512. MT9V032_WINDOW_WIDTH_MIN,
  513. MT9V032_WINDOW_WIDTH_MAX);
  514. rect.height = clamp_t(unsigned int, ALIGN(crop->rect.height, 2),
  515. MT9V032_WINDOW_HEIGHT_MIN,
  516. MT9V032_WINDOW_HEIGHT_MAX);
  517. rect.width = min_t(unsigned int,
  518. rect.width, MT9V032_PIXEL_ARRAY_WIDTH - rect.left);
  519. rect.height = min_t(unsigned int,
  520. rect.height, MT9V032_PIXEL_ARRAY_HEIGHT - rect.top);
  521. __crop = __mt9v032_get_pad_crop(mt9v032, fh, crop->pad, crop->which);
  522. if (rect.width != __crop->width || rect.height != __crop->height) {
  523. /* Reset the output image size if the crop rectangle size has
  524. * been modified.
  525. */
  526. __format = __mt9v032_get_pad_format(mt9v032, fh, crop->pad,
  527. crop->which);
  528. __format->width = rect.width;
  529. __format->height = rect.height;
  530. if (crop->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  531. mt9v032->hratio = 1;
  532. mt9v032->vratio = 1;
  533. mt9v032_configure_pixel_rate(mt9v032);
  534. }
  535. }
  536. *__crop = rect;
  537. crop->rect = rect;
  538. return 0;
  539. }
  540. /* -----------------------------------------------------------------------------
  541. * V4L2 subdev control operations
  542. */
  543. #define V4L2_CID_TEST_PATTERN_COLOR (V4L2_CID_USER_BASE | 0x1001)
  544. static int mt9v032_s_ctrl(struct v4l2_ctrl *ctrl)
  545. {
  546. struct mt9v032 *mt9v032 =
  547. container_of(ctrl->handler, struct mt9v032, ctrls);
  548. struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
  549. u32 freq;
  550. u16 data;
  551. switch (ctrl->id) {
  552. case V4L2_CID_AUTOGAIN:
  553. return mt9v032_update_aec_agc(mt9v032, MT9V032_AGC_ENABLE,
  554. ctrl->val);
  555. case V4L2_CID_GAIN:
  556. return mt9v032_write(client, MT9V032_ANALOG_GAIN, ctrl->val);
  557. case V4L2_CID_EXPOSURE_AUTO:
  558. return mt9v032_update_aec_agc(mt9v032, MT9V032_AEC_ENABLE,
  559. !ctrl->val);
  560. case V4L2_CID_EXPOSURE:
  561. return mt9v032_write(client, MT9V032_TOTAL_SHUTTER_WIDTH,
  562. ctrl->val);
  563. case V4L2_CID_HBLANK:
  564. mt9v032->hblank = ctrl->val;
  565. return mt9v032_update_hblank(mt9v032);
  566. case V4L2_CID_VBLANK:
  567. return mt9v032_write(client, MT9V032_VERTICAL_BLANKING,
  568. ctrl->val);
  569. case V4L2_CID_PIXEL_RATE:
  570. case V4L2_CID_LINK_FREQ:
  571. if (mt9v032->link_freq == NULL)
  572. break;
  573. freq = mt9v032->pdata->link_freqs[mt9v032->link_freq->val];
  574. mt9v032->pixel_rate->val64 = freq;
  575. mt9v032->sysclk = freq;
  576. break;
  577. case V4L2_CID_TEST_PATTERN:
  578. switch (mt9v032->test_pattern->val) {
  579. case 0:
  580. data = 0;
  581. break;
  582. case 1:
  583. data = MT9V032_TEST_PATTERN_GRAY_VERTICAL
  584. | MT9V032_TEST_PATTERN_ENABLE;
  585. break;
  586. case 2:
  587. data = MT9V032_TEST_PATTERN_GRAY_HORIZONTAL
  588. | MT9V032_TEST_PATTERN_ENABLE;
  589. break;
  590. case 3:
  591. data = MT9V032_TEST_PATTERN_GRAY_DIAGONAL
  592. | MT9V032_TEST_PATTERN_ENABLE;
  593. break;
  594. default:
  595. data = (mt9v032->test_pattern_color->val <<
  596. MT9V032_TEST_PATTERN_DATA_SHIFT)
  597. | MT9V032_TEST_PATTERN_USE_DATA
  598. | MT9V032_TEST_PATTERN_ENABLE
  599. | MT9V032_TEST_PATTERN_FLIP;
  600. break;
  601. }
  602. return mt9v032_write(client, MT9V032_TEST_PATTERN, data);
  603. }
  604. return 0;
  605. }
  606. static struct v4l2_ctrl_ops mt9v032_ctrl_ops = {
  607. .s_ctrl = mt9v032_s_ctrl,
  608. };
  609. static const char * const mt9v032_test_pattern_menu[] = {
  610. "Disabled",
  611. "Gray Vertical Shade",
  612. "Gray Horizontal Shade",
  613. "Gray Diagonal Shade",
  614. "Plain",
  615. };
  616. static const struct v4l2_ctrl_config mt9v032_test_pattern_color = {
  617. .ops = &mt9v032_ctrl_ops,
  618. .id = V4L2_CID_TEST_PATTERN_COLOR,
  619. .type = V4L2_CTRL_TYPE_INTEGER,
  620. .name = "Test Pattern Color",
  621. .min = 0,
  622. .max = 1023,
  623. .step = 1,
  624. .def = 0,
  625. .flags = 0,
  626. };
  627. /* -----------------------------------------------------------------------------
  628. * V4L2 subdev core operations
  629. */
  630. static int mt9v032_set_power(struct v4l2_subdev *subdev, int on)
  631. {
  632. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  633. int ret = 0;
  634. mutex_lock(&mt9v032->power_lock);
  635. /* If the power count is modified from 0 to != 0 or from != 0 to 0,
  636. * update the power state.
  637. */
  638. if (mt9v032->power_count == !on) {
  639. ret = __mt9v032_set_power(mt9v032, !!on);
  640. if (ret < 0)
  641. goto done;
  642. }
  643. /* Update the power count. */
  644. mt9v032->power_count += on ? 1 : -1;
  645. WARN_ON(mt9v032->power_count < 0);
  646. done:
  647. mutex_unlock(&mt9v032->power_lock);
  648. return ret;
  649. }
  650. /* -----------------------------------------------------------------------------
  651. * V4L2 subdev internal operations
  652. */
  653. static int mt9v032_registered(struct v4l2_subdev *subdev)
  654. {
  655. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  656. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  657. unsigned int i;
  658. s32 version;
  659. int ret;
  660. dev_info(&client->dev, "Probing MT9V032 at address 0x%02x\n",
  661. client->addr);
  662. ret = mt9v032_power_on(mt9v032);
  663. if (ret < 0) {
  664. dev_err(&client->dev, "MT9V032 power up failed\n");
  665. return ret;
  666. }
  667. /* Read and check the sensor version */
  668. version = mt9v032_read(client, MT9V032_CHIP_VERSION);
  669. if (version < 0) {
  670. dev_err(&client->dev, "Failed reading chip version\n");
  671. return version;
  672. }
  673. for (i = 0; i < ARRAY_SIZE(mt9v032_versions); ++i) {
  674. if (mt9v032_versions[i].version == version) {
  675. mt9v032->version = &mt9v032_versions[i];
  676. break;
  677. }
  678. }
  679. if (mt9v032->version == NULL) {
  680. dev_err(&client->dev, "Unsupported chip version 0x%04x\n",
  681. version);
  682. return -ENODEV;
  683. }
  684. mt9v032_power_off(mt9v032);
  685. dev_info(&client->dev, "%s detected at address 0x%02x\n",
  686. mt9v032->version->name, client->addr);
  687. mt9v032_configure_pixel_rate(mt9v032);
  688. return ret;
  689. }
  690. static int mt9v032_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
  691. {
  692. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  693. struct v4l2_mbus_framefmt *format;
  694. struct v4l2_rect *crop;
  695. crop = v4l2_subdev_get_try_crop(fh, 0);
  696. crop->left = MT9V032_COLUMN_START_DEF;
  697. crop->top = MT9V032_ROW_START_DEF;
  698. crop->width = MT9V032_WINDOW_WIDTH_DEF;
  699. crop->height = MT9V032_WINDOW_HEIGHT_DEF;
  700. format = v4l2_subdev_get_try_format(fh, 0);
  701. if (mt9v032->model->color)
  702. format->code = V4L2_MBUS_FMT_SGRBG10_1X10;
  703. else
  704. format->code = V4L2_MBUS_FMT_Y10_1X10;
  705. format->width = MT9V032_WINDOW_WIDTH_DEF;
  706. format->height = MT9V032_WINDOW_HEIGHT_DEF;
  707. format->field = V4L2_FIELD_NONE;
  708. format->colorspace = V4L2_COLORSPACE_SRGB;
  709. return mt9v032_set_power(subdev, 1);
  710. }
  711. static int mt9v032_close(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
  712. {
  713. return mt9v032_set_power(subdev, 0);
  714. }
  715. static struct v4l2_subdev_core_ops mt9v032_subdev_core_ops = {
  716. .s_power = mt9v032_set_power,
  717. };
  718. static struct v4l2_subdev_video_ops mt9v032_subdev_video_ops = {
  719. .s_stream = mt9v032_s_stream,
  720. };
  721. static struct v4l2_subdev_pad_ops mt9v032_subdev_pad_ops = {
  722. .enum_mbus_code = mt9v032_enum_mbus_code,
  723. .enum_frame_size = mt9v032_enum_frame_size,
  724. .get_fmt = mt9v032_get_format,
  725. .set_fmt = mt9v032_set_format,
  726. .get_crop = mt9v032_get_crop,
  727. .set_crop = mt9v032_set_crop,
  728. };
  729. static struct v4l2_subdev_ops mt9v032_subdev_ops = {
  730. .core = &mt9v032_subdev_core_ops,
  731. .video = &mt9v032_subdev_video_ops,
  732. .pad = &mt9v032_subdev_pad_ops,
  733. };
  734. static const struct v4l2_subdev_internal_ops mt9v032_subdev_internal_ops = {
  735. .registered = mt9v032_registered,
  736. .open = mt9v032_open,
  737. .close = mt9v032_close,
  738. };
  739. /* -----------------------------------------------------------------------------
  740. * Driver initialization and probing
  741. */
  742. static int mt9v032_probe(struct i2c_client *client,
  743. const struct i2c_device_id *did)
  744. {
  745. struct mt9v032_platform_data *pdata = client->dev.platform_data;
  746. struct mt9v032 *mt9v032;
  747. unsigned int i;
  748. int ret;
  749. if (!i2c_check_functionality(client->adapter,
  750. I2C_FUNC_SMBUS_WORD_DATA)) {
  751. dev_warn(&client->adapter->dev,
  752. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  753. return -EIO;
  754. }
  755. mt9v032 = devm_kzalloc(&client->dev, sizeof(*mt9v032), GFP_KERNEL);
  756. if (!mt9v032)
  757. return -ENOMEM;
  758. mt9v032->clk = devm_clk_get(&client->dev, NULL);
  759. if (IS_ERR(mt9v032->clk))
  760. return PTR_ERR(mt9v032->clk);
  761. mutex_init(&mt9v032->power_lock);
  762. mt9v032->pdata = pdata;
  763. mt9v032->model = (const void *)did->driver_data;
  764. v4l2_ctrl_handler_init(&mt9v032->ctrls, 10);
  765. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  766. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  767. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  768. V4L2_CID_GAIN, MT9V032_ANALOG_GAIN_MIN,
  769. MT9V032_ANALOG_GAIN_MAX, 1, MT9V032_ANALOG_GAIN_DEF);
  770. v4l2_ctrl_new_std_menu(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  771. V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL, 0,
  772. V4L2_EXPOSURE_AUTO);
  773. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  774. V4L2_CID_EXPOSURE, mt9v032->model->data->min_shutter,
  775. mt9v032->model->data->max_shutter, 1,
  776. MT9V032_TOTAL_SHUTTER_WIDTH_DEF);
  777. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  778. V4L2_CID_HBLANK, mt9v032->model->data->min_hblank,
  779. MT9V032_HORIZONTAL_BLANKING_MAX, 1,
  780. MT9V032_HORIZONTAL_BLANKING_DEF);
  781. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  782. V4L2_CID_VBLANK, mt9v032->model->data->min_vblank,
  783. mt9v032->model->data->max_vblank, 1,
  784. MT9V032_VERTICAL_BLANKING_DEF);
  785. mt9v032->test_pattern = v4l2_ctrl_new_std_menu_items(&mt9v032->ctrls,
  786. &mt9v032_ctrl_ops, V4L2_CID_TEST_PATTERN,
  787. ARRAY_SIZE(mt9v032_test_pattern_menu) - 1, 0, 0,
  788. mt9v032_test_pattern_menu);
  789. mt9v032->test_pattern_color = v4l2_ctrl_new_custom(&mt9v032->ctrls,
  790. &mt9v032_test_pattern_color, NULL);
  791. v4l2_ctrl_cluster(2, &mt9v032->test_pattern);
  792. mt9v032->pixel_rate =
  793. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  794. V4L2_CID_PIXEL_RATE, 0, 0, 1, 0);
  795. if (pdata && pdata->link_freqs) {
  796. unsigned int def = 0;
  797. for (i = 0; pdata->link_freqs[i]; ++i) {
  798. if (pdata->link_freqs[i] == pdata->link_def_freq)
  799. def = i;
  800. }
  801. mt9v032->link_freq =
  802. v4l2_ctrl_new_int_menu(&mt9v032->ctrls,
  803. &mt9v032_ctrl_ops,
  804. V4L2_CID_LINK_FREQ, i - 1, def,
  805. pdata->link_freqs);
  806. v4l2_ctrl_cluster(2, &mt9v032->link_freq);
  807. }
  808. mt9v032->subdev.ctrl_handler = &mt9v032->ctrls;
  809. if (mt9v032->ctrls.error)
  810. printk(KERN_INFO "%s: control initialization error %d\n",
  811. __func__, mt9v032->ctrls.error);
  812. mt9v032->crop.left = MT9V032_COLUMN_START_DEF;
  813. mt9v032->crop.top = MT9V032_ROW_START_DEF;
  814. mt9v032->crop.width = MT9V032_WINDOW_WIDTH_DEF;
  815. mt9v032->crop.height = MT9V032_WINDOW_HEIGHT_DEF;
  816. if (mt9v032->model->color)
  817. mt9v032->format.code = V4L2_MBUS_FMT_SGRBG10_1X10;
  818. else
  819. mt9v032->format.code = V4L2_MBUS_FMT_Y10_1X10;
  820. mt9v032->format.width = MT9V032_WINDOW_WIDTH_DEF;
  821. mt9v032->format.height = MT9V032_WINDOW_HEIGHT_DEF;
  822. mt9v032->format.field = V4L2_FIELD_NONE;
  823. mt9v032->format.colorspace = V4L2_COLORSPACE_SRGB;
  824. mt9v032->hratio = 1;
  825. mt9v032->vratio = 1;
  826. mt9v032->aec_agc = MT9V032_AEC_ENABLE | MT9V032_AGC_ENABLE;
  827. mt9v032->hblank = MT9V032_HORIZONTAL_BLANKING_DEF;
  828. mt9v032->sysclk = MT9V032_SYSCLK_FREQ_DEF;
  829. v4l2_i2c_subdev_init(&mt9v032->subdev, client, &mt9v032_subdev_ops);
  830. mt9v032->subdev.internal_ops = &mt9v032_subdev_internal_ops;
  831. mt9v032->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  832. mt9v032->pad.flags = MEDIA_PAD_FL_SOURCE;
  833. ret = media_entity_init(&mt9v032->subdev.entity, 1, &mt9v032->pad, 0);
  834. if (ret < 0)
  835. v4l2_ctrl_handler_free(&mt9v032->ctrls);
  836. return ret;
  837. }
  838. static int mt9v032_remove(struct i2c_client *client)
  839. {
  840. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  841. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  842. v4l2_ctrl_handler_free(&mt9v032->ctrls);
  843. v4l2_device_unregister_subdev(subdev);
  844. media_entity_cleanup(&subdev->entity);
  845. return 0;
  846. }
  847. static const struct i2c_device_id mt9v032_id[] = {
  848. { "mt9v032", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V032_COLOR] },
  849. { "mt9v032m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V032_MONO] },
  850. { "mt9v034", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V034_COLOR] },
  851. { "mt9v034m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V034_MONO] },
  852. { }
  853. };
  854. MODULE_DEVICE_TABLE(i2c, mt9v032_id);
  855. static struct i2c_driver mt9v032_driver = {
  856. .driver = {
  857. .name = "mt9v032",
  858. },
  859. .probe = mt9v032_probe,
  860. .remove = mt9v032_remove,
  861. .id_table = mt9v032_id,
  862. };
  863. module_i2c_driver(mt9v032_driver);
  864. MODULE_DESCRIPTION("Aptina MT9V032 Camera driver");
  865. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  866. MODULE_LICENSE("GPL");