mt9v032.c 29 KB

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