mt9v032.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  1. /*
  2. * Driver for MT9V022, MT9V024, MT9V032, and MT9V034 CMOS Image Sensors
  3. *
  4. * Copyright (C) 2010, Laurent Pinchart <[email protected]>
  5. *
  6. * Based on the MT9M001 driver,
  7. *
  8. * Copyright (C) 2008, Guennadi Liakhovetski <[email protected]>
  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/gpio/consumer.h>
  17. #include <linux/i2c.h>
  18. #include <linux/log2.h>
  19. #include <linux/mutex.h>
  20. #include <linux/of.h>
  21. #include <linux/regmap.h>
  22. #include <linux/slab.h>
  23. #include <linux/videodev2.h>
  24. #include <linux/v4l2-mediabus.h>
  25. #include <linux/module.h>
  26. #include <media/i2c/mt9v032.h>
  27. #include <media/v4l2-ctrls.h>
  28. #include <media/v4l2-device.h>
  29. #include <media/v4l2-of.h>
  30. #include <media/v4l2-subdev.h>
  31. /* The first four rows are black rows. The active area spans 753x481 pixels. */
  32. #define MT9V032_PIXEL_ARRAY_HEIGHT 485
  33. #define MT9V032_PIXEL_ARRAY_WIDTH 753
  34. #define MT9V032_SYSCLK_FREQ_DEF 26600000
  35. #define MT9V032_CHIP_VERSION 0x00
  36. #define MT9V032_CHIP_ID_REV1 0x1311
  37. #define MT9V032_CHIP_ID_REV3 0x1313
  38. #define MT9V034_CHIP_ID_REV1 0X1324
  39. #define MT9V032_COLUMN_START 0x01
  40. #define MT9V032_COLUMN_START_MIN 1
  41. #define MT9V032_COLUMN_START_DEF 1
  42. #define MT9V032_COLUMN_START_MAX 752
  43. #define MT9V032_ROW_START 0x02
  44. #define MT9V032_ROW_START_MIN 4
  45. #define MT9V032_ROW_START_DEF 5
  46. #define MT9V032_ROW_START_MAX 482
  47. #define MT9V032_WINDOW_HEIGHT 0x03
  48. #define MT9V032_WINDOW_HEIGHT_MIN 1
  49. #define MT9V032_WINDOW_HEIGHT_DEF 480
  50. #define MT9V032_WINDOW_HEIGHT_MAX 480
  51. #define MT9V032_WINDOW_WIDTH 0x04
  52. #define MT9V032_WINDOW_WIDTH_MIN 1
  53. #define MT9V032_WINDOW_WIDTH_DEF 752
  54. #define MT9V032_WINDOW_WIDTH_MAX 752
  55. #define MT9V032_HORIZONTAL_BLANKING 0x05
  56. #define MT9V032_HORIZONTAL_BLANKING_MIN 43
  57. #define MT9V034_HORIZONTAL_BLANKING_MIN 61
  58. #define MT9V032_HORIZONTAL_BLANKING_DEF 94
  59. #define MT9V032_HORIZONTAL_BLANKING_MAX 1023
  60. #define MT9V032_VERTICAL_BLANKING 0x06
  61. #define MT9V032_VERTICAL_BLANKING_MIN 4
  62. #define MT9V034_VERTICAL_BLANKING_MIN 2
  63. #define MT9V032_VERTICAL_BLANKING_DEF 45
  64. #define MT9V032_VERTICAL_BLANKING_MAX 3000
  65. #define MT9V034_VERTICAL_BLANKING_MAX 32288
  66. #define MT9V032_CHIP_CONTROL 0x07
  67. #define MT9V032_CHIP_CONTROL_MASTER_MODE (1 << 3)
  68. #define MT9V032_CHIP_CONTROL_DOUT_ENABLE (1 << 7)
  69. #define MT9V032_CHIP_CONTROL_SEQUENTIAL (1 << 8)
  70. #define MT9V032_SHUTTER_WIDTH1 0x08
  71. #define MT9V032_SHUTTER_WIDTH2 0x09
  72. #define MT9V032_SHUTTER_WIDTH_CONTROL 0x0a
  73. #define MT9V032_TOTAL_SHUTTER_WIDTH 0x0b
  74. #define MT9V032_TOTAL_SHUTTER_WIDTH_MIN 1
  75. #define MT9V034_TOTAL_SHUTTER_WIDTH_MIN 0
  76. #define MT9V032_TOTAL_SHUTTER_WIDTH_DEF 480
  77. #define MT9V032_TOTAL_SHUTTER_WIDTH_MAX 32767
  78. #define MT9V034_TOTAL_SHUTTER_WIDTH_MAX 32765
  79. #define MT9V032_RESET 0x0c
  80. #define MT9V032_READ_MODE 0x0d
  81. #define MT9V032_READ_MODE_ROW_BIN_MASK (3 << 0)
  82. #define MT9V032_READ_MODE_ROW_BIN_SHIFT 0
  83. #define MT9V032_READ_MODE_COLUMN_BIN_MASK (3 << 2)
  84. #define MT9V032_READ_MODE_COLUMN_BIN_SHIFT 2
  85. #define MT9V032_READ_MODE_ROW_FLIP (1 << 4)
  86. #define MT9V032_READ_MODE_COLUMN_FLIP (1 << 5)
  87. #define MT9V032_READ_MODE_DARK_COLUMNS (1 << 6)
  88. #define MT9V032_READ_MODE_DARK_ROWS (1 << 7)
  89. #define MT9V032_READ_MODE_RESERVED 0x0300
  90. #define MT9V032_PIXEL_OPERATION_MODE 0x0f
  91. #define MT9V034_PIXEL_OPERATION_MODE_HDR (1 << 0)
  92. #define MT9V034_PIXEL_OPERATION_MODE_COLOR (1 << 1)
  93. #define MT9V032_PIXEL_OPERATION_MODE_COLOR (1 << 2)
  94. #define MT9V032_PIXEL_OPERATION_MODE_HDR (1 << 6)
  95. #define MT9V032_ANALOG_GAIN 0x35
  96. #define MT9V032_ANALOG_GAIN_MIN 16
  97. #define MT9V032_ANALOG_GAIN_DEF 16
  98. #define MT9V032_ANALOG_GAIN_MAX 64
  99. #define MT9V032_MAX_ANALOG_GAIN 0x36
  100. #define MT9V032_MAX_ANALOG_GAIN_MAX 127
  101. #define MT9V032_FRAME_DARK_AVERAGE 0x42
  102. #define MT9V032_DARK_AVG_THRESH 0x46
  103. #define MT9V032_DARK_AVG_LOW_THRESH_MASK (255 << 0)
  104. #define MT9V032_DARK_AVG_LOW_THRESH_SHIFT 0
  105. #define MT9V032_DARK_AVG_HIGH_THRESH_MASK (255 << 8)
  106. #define MT9V032_DARK_AVG_HIGH_THRESH_SHIFT 8
  107. #define MT9V032_ROW_NOISE_CORR_CONTROL 0x70
  108. #define MT9V034_ROW_NOISE_CORR_ENABLE (1 << 0)
  109. #define MT9V034_ROW_NOISE_CORR_USE_BLK_AVG (1 << 1)
  110. #define MT9V032_ROW_NOISE_CORR_ENABLE (1 << 5)
  111. #define MT9V032_ROW_NOISE_CORR_USE_BLK_AVG (1 << 7)
  112. #define MT9V032_PIXEL_CLOCK 0x74
  113. #define MT9V034_PIXEL_CLOCK 0x72
  114. #define MT9V032_PIXEL_CLOCK_INV_LINE (1 << 0)
  115. #define MT9V032_PIXEL_CLOCK_INV_FRAME (1 << 1)
  116. #define MT9V032_PIXEL_CLOCK_XOR_LINE (1 << 2)
  117. #define MT9V032_PIXEL_CLOCK_CONT_LINE (1 << 3)
  118. #define MT9V032_PIXEL_CLOCK_INV_PXL_CLK (1 << 4)
  119. #define MT9V032_TEST_PATTERN 0x7f
  120. #define MT9V032_TEST_PATTERN_DATA_MASK (1023 << 0)
  121. #define MT9V032_TEST_PATTERN_DATA_SHIFT 0
  122. #define MT9V032_TEST_PATTERN_USE_DATA (1 << 10)
  123. #define MT9V032_TEST_PATTERN_GRAY_MASK (3 << 11)
  124. #define MT9V032_TEST_PATTERN_GRAY_NONE (0 << 11)
  125. #define MT9V032_TEST_PATTERN_GRAY_VERTICAL (1 << 11)
  126. #define MT9V032_TEST_PATTERN_GRAY_HORIZONTAL (2 << 11)
  127. #define MT9V032_TEST_PATTERN_GRAY_DIAGONAL (3 << 11)
  128. #define MT9V032_TEST_PATTERN_ENABLE (1 << 13)
  129. #define MT9V032_TEST_PATTERN_FLIP (1 << 14)
  130. #define MT9V032_AEGC_DESIRED_BIN 0xa5
  131. #define MT9V032_AEC_UPDATE_FREQUENCY 0xa6
  132. #define MT9V032_AEC_LPF 0xa8
  133. #define MT9V032_AGC_UPDATE_FREQUENCY 0xa9
  134. #define MT9V032_AGC_LPF 0xaa
  135. #define MT9V032_AEC_AGC_ENABLE 0xaf
  136. #define MT9V032_AEC_ENABLE (1 << 0)
  137. #define MT9V032_AGC_ENABLE (1 << 1)
  138. #define MT9V034_AEC_MAX_SHUTTER_WIDTH 0xad
  139. #define MT9V032_AEC_MAX_SHUTTER_WIDTH 0xbd
  140. #define MT9V032_THERMAL_INFO 0xc1
  141. enum mt9v032_model {
  142. MT9V032_MODEL_V022_COLOR, /* MT9V022IX7ATC */
  143. MT9V032_MODEL_V022_MONO, /* MT9V022IX7ATM */
  144. MT9V032_MODEL_V024_COLOR, /* MT9V024IA7XTC */
  145. MT9V032_MODEL_V024_MONO, /* MT9V024IA7XTM */
  146. MT9V032_MODEL_V032_COLOR, /* MT9V032C12STM */
  147. MT9V032_MODEL_V032_MONO, /* MT9V032C12STC */
  148. MT9V032_MODEL_V034_COLOR,
  149. MT9V032_MODEL_V034_MONO,
  150. };
  151. struct mt9v032_model_version {
  152. unsigned int version;
  153. const char *name;
  154. };
  155. struct mt9v032_model_data {
  156. unsigned int min_row_time;
  157. unsigned int min_hblank;
  158. unsigned int min_vblank;
  159. unsigned int max_vblank;
  160. unsigned int min_shutter;
  161. unsigned int max_shutter;
  162. unsigned int pclk_reg;
  163. unsigned int aec_max_shutter_reg;
  164. const struct v4l2_ctrl_config * const aec_max_shutter_v4l2_ctrl;
  165. };
  166. struct mt9v032_model_info {
  167. const struct mt9v032_model_data *data;
  168. bool color;
  169. };
  170. static const struct mt9v032_model_version mt9v032_versions[] = {
  171. { MT9V032_CHIP_ID_REV1, "MT9V022/MT9V032 rev1/2" },
  172. { MT9V032_CHIP_ID_REV3, "MT9V022/MT9V032 rev3" },
  173. { MT9V034_CHIP_ID_REV1, "MT9V024/MT9V034 rev1" },
  174. };
  175. struct mt9v032 {
  176. struct v4l2_subdev subdev;
  177. struct media_pad pad;
  178. struct v4l2_mbus_framefmt format;
  179. struct v4l2_rect crop;
  180. unsigned int hratio;
  181. unsigned int vratio;
  182. struct v4l2_ctrl_handler ctrls;
  183. struct {
  184. struct v4l2_ctrl *link_freq;
  185. struct v4l2_ctrl *pixel_rate;
  186. };
  187. struct mutex power_lock;
  188. int power_count;
  189. struct regmap *regmap;
  190. struct clk *clk;
  191. struct gpio_desc *reset_gpio;
  192. struct gpio_desc *standby_gpio;
  193. struct mt9v032_platform_data *pdata;
  194. const struct mt9v032_model_info *model;
  195. const struct mt9v032_model_version *version;
  196. u32 sysclk;
  197. u16 aec_agc;
  198. u16 hblank;
  199. struct {
  200. struct v4l2_ctrl *test_pattern;
  201. struct v4l2_ctrl *test_pattern_color;
  202. };
  203. };
  204. static struct mt9v032 *to_mt9v032(struct v4l2_subdev *sd)
  205. {
  206. return container_of(sd, struct mt9v032, subdev);
  207. }
  208. static int
  209. mt9v032_update_aec_agc(struct mt9v032 *mt9v032, u16 which, int enable)
  210. {
  211. struct regmap *map = mt9v032->regmap;
  212. u16 value = mt9v032->aec_agc;
  213. int ret;
  214. if (enable)
  215. value |= which;
  216. else
  217. value &= ~which;
  218. ret = regmap_write(map, MT9V032_AEC_AGC_ENABLE, value);
  219. if (ret < 0)
  220. return ret;
  221. mt9v032->aec_agc = value;
  222. return 0;
  223. }
  224. static int
  225. mt9v032_update_hblank(struct mt9v032 *mt9v032)
  226. {
  227. struct v4l2_rect *crop = &mt9v032->crop;
  228. unsigned int min_hblank = mt9v032->model->data->min_hblank;
  229. unsigned int hblank;
  230. if (mt9v032->version->version == MT9V034_CHIP_ID_REV1)
  231. min_hblank += (mt9v032->hratio - 1) * 10;
  232. min_hblank = max_t(int, mt9v032->model->data->min_row_time - crop->width,
  233. min_hblank);
  234. hblank = max_t(unsigned int, mt9v032->hblank, min_hblank);
  235. return regmap_write(mt9v032->regmap, MT9V032_HORIZONTAL_BLANKING,
  236. hblank);
  237. }
  238. static int mt9v032_power_on(struct mt9v032 *mt9v032)
  239. {
  240. struct regmap *map = mt9v032->regmap;
  241. int ret;
  242. if (mt9v032->reset_gpio)
  243. gpiod_set_value_cansleep(mt9v032->reset_gpio, 1);
  244. ret = clk_set_rate(mt9v032->clk, mt9v032->sysclk);
  245. if (ret < 0)
  246. return ret;
  247. /* System clock has to be enabled before releasing the reset */
  248. ret = clk_prepare_enable(mt9v032->clk);
  249. if (ret)
  250. return ret;
  251. udelay(1);
  252. if (mt9v032->reset_gpio) {
  253. gpiod_set_value_cansleep(mt9v032->reset_gpio, 0);
  254. /* After releasing reset we need to wait 10 clock cycles
  255. * before accessing the sensor over I2C. As the minimum SYSCLK
  256. * frequency is 13MHz, waiting 1µs will be enough in the worst
  257. * case.
  258. */
  259. udelay(1);
  260. }
  261. /* Reset the chip and stop data read out */
  262. ret = regmap_write(map, MT9V032_RESET, 1);
  263. if (ret < 0)
  264. return ret;
  265. ret = regmap_write(map, MT9V032_RESET, 0);
  266. if (ret < 0)
  267. return ret;
  268. return regmap_write(map, MT9V032_CHIP_CONTROL,
  269. MT9V032_CHIP_CONTROL_MASTER_MODE);
  270. }
  271. static void mt9v032_power_off(struct mt9v032 *mt9v032)
  272. {
  273. clk_disable_unprepare(mt9v032->clk);
  274. }
  275. static int __mt9v032_set_power(struct mt9v032 *mt9v032, bool on)
  276. {
  277. struct regmap *map = mt9v032->regmap;
  278. int ret;
  279. if (!on) {
  280. mt9v032_power_off(mt9v032);
  281. return 0;
  282. }
  283. ret = mt9v032_power_on(mt9v032);
  284. if (ret < 0)
  285. return ret;
  286. /* Configure the pixel clock polarity */
  287. if (mt9v032->pdata && mt9v032->pdata->clk_pol) {
  288. ret = regmap_write(map, mt9v032->model->data->pclk_reg,
  289. MT9V032_PIXEL_CLOCK_INV_PXL_CLK);
  290. if (ret < 0)
  291. return ret;
  292. }
  293. /* Disable the noise correction algorithm and restore the controls. */
  294. ret = regmap_write(map, MT9V032_ROW_NOISE_CORR_CONTROL, 0);
  295. if (ret < 0)
  296. return ret;
  297. return v4l2_ctrl_handler_setup(&mt9v032->ctrls);
  298. }
  299. /* -----------------------------------------------------------------------------
  300. * V4L2 subdev video operations
  301. */
  302. static struct v4l2_mbus_framefmt *
  303. __mt9v032_get_pad_format(struct mt9v032 *mt9v032, struct v4l2_subdev_pad_config *cfg,
  304. unsigned int pad, enum v4l2_subdev_format_whence which)
  305. {
  306. switch (which) {
  307. case V4L2_SUBDEV_FORMAT_TRY:
  308. return v4l2_subdev_get_try_format(&mt9v032->subdev, cfg, pad);
  309. case V4L2_SUBDEV_FORMAT_ACTIVE:
  310. return &mt9v032->format;
  311. default:
  312. return NULL;
  313. }
  314. }
  315. static struct v4l2_rect *
  316. __mt9v032_get_pad_crop(struct mt9v032 *mt9v032, struct v4l2_subdev_pad_config *cfg,
  317. unsigned int pad, enum v4l2_subdev_format_whence which)
  318. {
  319. switch (which) {
  320. case V4L2_SUBDEV_FORMAT_TRY:
  321. return v4l2_subdev_get_try_crop(&mt9v032->subdev, cfg, pad);
  322. case V4L2_SUBDEV_FORMAT_ACTIVE:
  323. return &mt9v032->crop;
  324. default:
  325. return NULL;
  326. }
  327. }
  328. static int mt9v032_s_stream(struct v4l2_subdev *subdev, int enable)
  329. {
  330. const u16 mode = MT9V032_CHIP_CONTROL_DOUT_ENABLE
  331. | MT9V032_CHIP_CONTROL_SEQUENTIAL;
  332. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  333. struct v4l2_rect *crop = &mt9v032->crop;
  334. struct regmap *map = mt9v032->regmap;
  335. unsigned int hbin;
  336. unsigned int vbin;
  337. int ret;
  338. if (!enable)
  339. return regmap_update_bits(map, MT9V032_CHIP_CONTROL, mode, 0);
  340. /* Configure the window size and row/column bin */
  341. hbin = fls(mt9v032->hratio) - 1;
  342. vbin = fls(mt9v032->vratio) - 1;
  343. ret = regmap_update_bits(map, MT9V032_READ_MODE,
  344. ~MT9V032_READ_MODE_RESERVED,
  345. hbin << MT9V032_READ_MODE_COLUMN_BIN_SHIFT |
  346. vbin << MT9V032_READ_MODE_ROW_BIN_SHIFT);
  347. if (ret < 0)
  348. return ret;
  349. ret = regmap_write(map, MT9V032_COLUMN_START, crop->left);
  350. if (ret < 0)
  351. return ret;
  352. ret = regmap_write(map, MT9V032_ROW_START, crop->top);
  353. if (ret < 0)
  354. return ret;
  355. ret = regmap_write(map, MT9V032_WINDOW_WIDTH, crop->width);
  356. if (ret < 0)
  357. return ret;
  358. ret = regmap_write(map, MT9V032_WINDOW_HEIGHT, crop->height);
  359. if (ret < 0)
  360. return ret;
  361. ret = mt9v032_update_hblank(mt9v032);
  362. if (ret < 0)
  363. return ret;
  364. /* Switch to master "normal" mode */
  365. return regmap_update_bits(map, MT9V032_CHIP_CONTROL, mode, mode);
  366. }
  367. static int mt9v032_enum_mbus_code(struct v4l2_subdev *subdev,
  368. struct v4l2_subdev_pad_config *cfg,
  369. struct v4l2_subdev_mbus_code_enum *code)
  370. {
  371. if (code->index > 0)
  372. return -EINVAL;
  373. code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
  374. return 0;
  375. }
  376. static int mt9v032_enum_frame_size(struct v4l2_subdev *subdev,
  377. struct v4l2_subdev_pad_config *cfg,
  378. struct v4l2_subdev_frame_size_enum *fse)
  379. {
  380. if (fse->index >= 3 || fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
  381. return -EINVAL;
  382. fse->min_width = MT9V032_WINDOW_WIDTH_DEF / (1 << fse->index);
  383. fse->max_width = fse->min_width;
  384. fse->min_height = MT9V032_WINDOW_HEIGHT_DEF / (1 << fse->index);
  385. fse->max_height = fse->min_height;
  386. return 0;
  387. }
  388. static int mt9v032_get_format(struct v4l2_subdev *subdev,
  389. struct v4l2_subdev_pad_config *cfg,
  390. struct v4l2_subdev_format *format)
  391. {
  392. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  393. format->format = *__mt9v032_get_pad_format(mt9v032, cfg, format->pad,
  394. format->which);
  395. return 0;
  396. }
  397. static void mt9v032_configure_pixel_rate(struct mt9v032 *mt9v032)
  398. {
  399. struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
  400. int ret;
  401. ret = v4l2_ctrl_s_ctrl_int64(mt9v032->pixel_rate,
  402. mt9v032->sysclk / mt9v032->hratio);
  403. if (ret < 0)
  404. dev_warn(&client->dev, "failed to set pixel rate (%d)\n", ret);
  405. }
  406. static unsigned int mt9v032_calc_ratio(unsigned int input, unsigned int output)
  407. {
  408. /* Compute the power-of-two binning factor closest to the input size to
  409. * output size ratio. Given that the output size is bounded by input/4
  410. * and input, a generic implementation would be an ineffective luxury.
  411. */
  412. if (output * 3 > input * 2)
  413. return 1;
  414. if (output * 3 > input)
  415. return 2;
  416. return 4;
  417. }
  418. static int mt9v032_set_format(struct v4l2_subdev *subdev,
  419. struct v4l2_subdev_pad_config *cfg,
  420. struct v4l2_subdev_format *format)
  421. {
  422. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  423. struct v4l2_mbus_framefmt *__format;
  424. struct v4l2_rect *__crop;
  425. unsigned int width;
  426. unsigned int height;
  427. unsigned int hratio;
  428. unsigned int vratio;
  429. __crop = __mt9v032_get_pad_crop(mt9v032, cfg, format->pad,
  430. format->which);
  431. /* Clamp the width and height to avoid dividing by zero. */
  432. width = clamp(ALIGN(format->format.width, 2),
  433. max_t(unsigned int, __crop->width / 4,
  434. MT9V032_WINDOW_WIDTH_MIN),
  435. __crop->width);
  436. height = clamp(ALIGN(format->format.height, 2),
  437. max_t(unsigned int, __crop->height / 4,
  438. MT9V032_WINDOW_HEIGHT_MIN),
  439. __crop->height);
  440. hratio = mt9v032_calc_ratio(__crop->width, width);
  441. vratio = mt9v032_calc_ratio(__crop->height, height);
  442. __format = __mt9v032_get_pad_format(mt9v032, cfg, format->pad,
  443. format->which);
  444. __format->width = __crop->width / hratio;
  445. __format->height = __crop->height / vratio;
  446. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  447. mt9v032->hratio = hratio;
  448. mt9v032->vratio = vratio;
  449. mt9v032_configure_pixel_rate(mt9v032);
  450. }
  451. format->format = *__format;
  452. return 0;
  453. }
  454. static int mt9v032_get_selection(struct v4l2_subdev *subdev,
  455. struct v4l2_subdev_pad_config *cfg,
  456. struct v4l2_subdev_selection *sel)
  457. {
  458. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  459. if (sel->target != V4L2_SEL_TGT_CROP)
  460. return -EINVAL;
  461. sel->r = *__mt9v032_get_pad_crop(mt9v032, cfg, sel->pad, sel->which);
  462. return 0;
  463. }
  464. static int mt9v032_set_selection(struct v4l2_subdev *subdev,
  465. struct v4l2_subdev_pad_config *cfg,
  466. struct v4l2_subdev_selection *sel)
  467. {
  468. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  469. struct v4l2_mbus_framefmt *__format;
  470. struct v4l2_rect *__crop;
  471. struct v4l2_rect rect;
  472. if (sel->target != V4L2_SEL_TGT_CROP)
  473. return -EINVAL;
  474. /* Clamp the crop rectangle boundaries and align them to a non multiple
  475. * of 2 pixels to ensure a GRBG Bayer pattern.
  476. */
  477. rect.left = clamp(ALIGN(sel->r.left + 1, 2) - 1,
  478. MT9V032_COLUMN_START_MIN,
  479. MT9V032_COLUMN_START_MAX);
  480. rect.top = clamp(ALIGN(sel->r.top + 1, 2) - 1,
  481. MT9V032_ROW_START_MIN,
  482. MT9V032_ROW_START_MAX);
  483. rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
  484. MT9V032_WINDOW_WIDTH_MIN,
  485. MT9V032_WINDOW_WIDTH_MAX);
  486. rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
  487. MT9V032_WINDOW_HEIGHT_MIN,
  488. MT9V032_WINDOW_HEIGHT_MAX);
  489. rect.width = min_t(unsigned int,
  490. rect.width, MT9V032_PIXEL_ARRAY_WIDTH - rect.left);
  491. rect.height = min_t(unsigned int,
  492. rect.height, MT9V032_PIXEL_ARRAY_HEIGHT - rect.top);
  493. __crop = __mt9v032_get_pad_crop(mt9v032, cfg, sel->pad, sel->which);
  494. if (rect.width != __crop->width || rect.height != __crop->height) {
  495. /* Reset the output image size if the crop rectangle size has
  496. * been modified.
  497. */
  498. __format = __mt9v032_get_pad_format(mt9v032, cfg, sel->pad,
  499. sel->which);
  500. __format->width = rect.width;
  501. __format->height = rect.height;
  502. if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  503. mt9v032->hratio = 1;
  504. mt9v032->vratio = 1;
  505. mt9v032_configure_pixel_rate(mt9v032);
  506. }
  507. }
  508. *__crop = rect;
  509. sel->r = rect;
  510. return 0;
  511. }
  512. /* -----------------------------------------------------------------------------
  513. * V4L2 subdev control operations
  514. */
  515. #define V4L2_CID_TEST_PATTERN_COLOR (V4L2_CID_USER_BASE | 0x1001)
  516. /*
  517. * Value between 1 and 64 to set the desired bin. This is effectively a measure
  518. * of how bright the image is supposed to be. Both AGC and AEC try to reach
  519. * this.
  520. */
  521. #define V4L2_CID_AEGC_DESIRED_BIN (V4L2_CID_USER_BASE | 0x1002)
  522. /*
  523. * LPF is the low pass filter capability of the chip. Both AEC and AGC have
  524. * this setting. This limits the speed in which AGC/AEC adjust their settings.
  525. * Possible values are 0-2. 0 means no LPF. For 1 and 2 this equation is used:
  526. *
  527. * if |(calculated new exp - current exp)| > (current exp / 4)
  528. * next exp = calculated new exp
  529. * else
  530. * next exp = current exp + ((calculated new exp - current exp) / 2^LPF)
  531. */
  532. #define V4L2_CID_AEC_LPF (V4L2_CID_USER_BASE | 0x1003)
  533. #define V4L2_CID_AGC_LPF (V4L2_CID_USER_BASE | 0x1004)
  534. /*
  535. * Value between 0 and 15. This is the number of frames being skipped before
  536. * updating the auto exposure/gain.
  537. */
  538. #define V4L2_CID_AEC_UPDATE_INTERVAL (V4L2_CID_USER_BASE | 0x1005)
  539. #define V4L2_CID_AGC_UPDATE_INTERVAL (V4L2_CID_USER_BASE | 0x1006)
  540. /*
  541. * Maximum shutter width used for AEC.
  542. */
  543. #define V4L2_CID_AEC_MAX_SHUTTER_WIDTH (V4L2_CID_USER_BASE | 0x1007)
  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 regmap *map = mt9v032->regmap;
  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 regmap_write(map, 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 regmap_write(map, 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 regmap_write(map, 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->p_new.p_s64 = 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 regmap_write(map, MT9V032_TEST_PATTERN, data);
  603. case V4L2_CID_AEGC_DESIRED_BIN:
  604. return regmap_write(map, MT9V032_AEGC_DESIRED_BIN, ctrl->val);
  605. case V4L2_CID_AEC_LPF:
  606. return regmap_write(map, MT9V032_AEC_LPF, ctrl->val);
  607. case V4L2_CID_AGC_LPF:
  608. return regmap_write(map, MT9V032_AGC_LPF, ctrl->val);
  609. case V4L2_CID_AEC_UPDATE_INTERVAL:
  610. return regmap_write(map, MT9V032_AEC_UPDATE_FREQUENCY,
  611. ctrl->val);
  612. case V4L2_CID_AGC_UPDATE_INTERVAL:
  613. return regmap_write(map, MT9V032_AGC_UPDATE_FREQUENCY,
  614. ctrl->val);
  615. case V4L2_CID_AEC_MAX_SHUTTER_WIDTH:
  616. return regmap_write(map,
  617. mt9v032->model->data->aec_max_shutter_reg,
  618. ctrl->val);
  619. }
  620. return 0;
  621. }
  622. static const struct v4l2_ctrl_ops mt9v032_ctrl_ops = {
  623. .s_ctrl = mt9v032_s_ctrl,
  624. };
  625. static const char * const mt9v032_test_pattern_menu[] = {
  626. "Disabled",
  627. "Gray Vertical Shade",
  628. "Gray Horizontal Shade",
  629. "Gray Diagonal Shade",
  630. "Plain",
  631. };
  632. static const struct v4l2_ctrl_config mt9v032_test_pattern_color = {
  633. .ops = &mt9v032_ctrl_ops,
  634. .id = V4L2_CID_TEST_PATTERN_COLOR,
  635. .type = V4L2_CTRL_TYPE_INTEGER,
  636. .name = "Test Pattern Color",
  637. .min = 0,
  638. .max = 1023,
  639. .step = 1,
  640. .def = 0,
  641. .flags = 0,
  642. };
  643. static const struct v4l2_ctrl_config mt9v032_aegc_controls[] = {
  644. {
  645. .ops = &mt9v032_ctrl_ops,
  646. .id = V4L2_CID_AEGC_DESIRED_BIN,
  647. .type = V4L2_CTRL_TYPE_INTEGER,
  648. .name = "AEC/AGC Desired Bin",
  649. .min = 1,
  650. .max = 64,
  651. .step = 1,
  652. .def = 58,
  653. .flags = 0,
  654. }, {
  655. .ops = &mt9v032_ctrl_ops,
  656. .id = V4L2_CID_AEC_LPF,
  657. .type = V4L2_CTRL_TYPE_INTEGER,
  658. .name = "AEC Low Pass Filter",
  659. .min = 0,
  660. .max = 2,
  661. .step = 1,
  662. .def = 0,
  663. .flags = 0,
  664. }, {
  665. .ops = &mt9v032_ctrl_ops,
  666. .id = V4L2_CID_AGC_LPF,
  667. .type = V4L2_CTRL_TYPE_INTEGER,
  668. .name = "AGC Low Pass Filter",
  669. .min = 0,
  670. .max = 2,
  671. .step = 1,
  672. .def = 2,
  673. .flags = 0,
  674. }, {
  675. .ops = &mt9v032_ctrl_ops,
  676. .id = V4L2_CID_AEC_UPDATE_INTERVAL,
  677. .type = V4L2_CTRL_TYPE_INTEGER,
  678. .name = "AEC Update Interval",
  679. .min = 0,
  680. .max = 16,
  681. .step = 1,
  682. .def = 2,
  683. .flags = 0,
  684. }, {
  685. .ops = &mt9v032_ctrl_ops,
  686. .id = V4L2_CID_AGC_UPDATE_INTERVAL,
  687. .type = V4L2_CTRL_TYPE_INTEGER,
  688. .name = "AGC Update Interval",
  689. .min = 0,
  690. .max = 16,
  691. .step = 1,
  692. .def = 2,
  693. .flags = 0,
  694. }
  695. };
  696. static const struct v4l2_ctrl_config mt9v032_aec_max_shutter_width = {
  697. .ops = &mt9v032_ctrl_ops,
  698. .id = V4L2_CID_AEC_MAX_SHUTTER_WIDTH,
  699. .type = V4L2_CTRL_TYPE_INTEGER,
  700. .name = "AEC Max Shutter Width",
  701. .min = 1,
  702. .max = 2047,
  703. .step = 1,
  704. .def = 480,
  705. .flags = 0,
  706. };
  707. static const struct v4l2_ctrl_config mt9v034_aec_max_shutter_width = {
  708. .ops = &mt9v032_ctrl_ops,
  709. .id = V4L2_CID_AEC_MAX_SHUTTER_WIDTH,
  710. .type = V4L2_CTRL_TYPE_INTEGER,
  711. .name = "AEC Max Shutter Width",
  712. .min = 1,
  713. .max = 32765,
  714. .step = 1,
  715. .def = 480,
  716. .flags = 0,
  717. };
  718. /* -----------------------------------------------------------------------------
  719. * V4L2 subdev core operations
  720. */
  721. static int mt9v032_set_power(struct v4l2_subdev *subdev, int on)
  722. {
  723. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  724. int ret = 0;
  725. mutex_lock(&mt9v032->power_lock);
  726. /* If the power count is modified from 0 to != 0 or from != 0 to 0,
  727. * update the power state.
  728. */
  729. if (mt9v032->power_count == !on) {
  730. ret = __mt9v032_set_power(mt9v032, !!on);
  731. if (ret < 0)
  732. goto done;
  733. }
  734. /* Update the power count. */
  735. mt9v032->power_count += on ? 1 : -1;
  736. WARN_ON(mt9v032->power_count < 0);
  737. done:
  738. mutex_unlock(&mt9v032->power_lock);
  739. return ret;
  740. }
  741. /* -----------------------------------------------------------------------------
  742. * V4L2 subdev internal operations
  743. */
  744. static int mt9v032_registered(struct v4l2_subdev *subdev)
  745. {
  746. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  747. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  748. unsigned int i;
  749. u32 version;
  750. int ret;
  751. dev_info(&client->dev, "Probing MT9V032 at address 0x%02x\n",
  752. client->addr);
  753. ret = mt9v032_power_on(mt9v032);
  754. if (ret < 0) {
  755. dev_err(&client->dev, "MT9V032 power up failed\n");
  756. return ret;
  757. }
  758. /* Read and check the sensor version */
  759. ret = regmap_read(mt9v032->regmap, MT9V032_CHIP_VERSION, &version);
  760. if (ret < 0) {
  761. dev_err(&client->dev, "Failed reading chip version\n");
  762. return ret;
  763. }
  764. for (i = 0; i < ARRAY_SIZE(mt9v032_versions); ++i) {
  765. if (mt9v032_versions[i].version == version) {
  766. mt9v032->version = &mt9v032_versions[i];
  767. break;
  768. }
  769. }
  770. if (mt9v032->version == NULL) {
  771. dev_err(&client->dev, "Unsupported chip version 0x%04x\n",
  772. version);
  773. return -ENODEV;
  774. }
  775. mt9v032_power_off(mt9v032);
  776. dev_info(&client->dev, "%s detected at address 0x%02x\n",
  777. mt9v032->version->name, client->addr);
  778. mt9v032_configure_pixel_rate(mt9v032);
  779. return ret;
  780. }
  781. static int mt9v032_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
  782. {
  783. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  784. struct v4l2_mbus_framefmt *format;
  785. struct v4l2_rect *crop;
  786. crop = v4l2_subdev_get_try_crop(subdev, fh->pad, 0);
  787. crop->left = MT9V032_COLUMN_START_DEF;
  788. crop->top = MT9V032_ROW_START_DEF;
  789. crop->width = MT9V032_WINDOW_WIDTH_DEF;
  790. crop->height = MT9V032_WINDOW_HEIGHT_DEF;
  791. format = v4l2_subdev_get_try_format(subdev, fh->pad, 0);
  792. if (mt9v032->model->color)
  793. format->code = MEDIA_BUS_FMT_SGRBG10_1X10;
  794. else
  795. format->code = MEDIA_BUS_FMT_Y10_1X10;
  796. format->width = MT9V032_WINDOW_WIDTH_DEF;
  797. format->height = MT9V032_WINDOW_HEIGHT_DEF;
  798. format->field = V4L2_FIELD_NONE;
  799. format->colorspace = V4L2_COLORSPACE_SRGB;
  800. return mt9v032_set_power(subdev, 1);
  801. }
  802. static int mt9v032_close(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
  803. {
  804. return mt9v032_set_power(subdev, 0);
  805. }
  806. static struct v4l2_subdev_core_ops mt9v032_subdev_core_ops = {
  807. .s_power = mt9v032_set_power,
  808. };
  809. static struct v4l2_subdev_video_ops mt9v032_subdev_video_ops = {
  810. .s_stream = mt9v032_s_stream,
  811. };
  812. static struct v4l2_subdev_pad_ops mt9v032_subdev_pad_ops = {
  813. .enum_mbus_code = mt9v032_enum_mbus_code,
  814. .enum_frame_size = mt9v032_enum_frame_size,
  815. .get_fmt = mt9v032_get_format,
  816. .set_fmt = mt9v032_set_format,
  817. .get_selection = mt9v032_get_selection,
  818. .set_selection = mt9v032_set_selection,
  819. };
  820. static struct v4l2_subdev_ops mt9v032_subdev_ops = {
  821. .core = &mt9v032_subdev_core_ops,
  822. .video = &mt9v032_subdev_video_ops,
  823. .pad = &mt9v032_subdev_pad_ops,
  824. };
  825. static const struct v4l2_subdev_internal_ops mt9v032_subdev_internal_ops = {
  826. .registered = mt9v032_registered,
  827. .open = mt9v032_open,
  828. .close = mt9v032_close,
  829. };
  830. static const struct regmap_config mt9v032_regmap_config = {
  831. .reg_bits = 8,
  832. .val_bits = 16,
  833. .max_register = 0xff,
  834. .cache_type = REGCACHE_RBTREE,
  835. };
  836. /* -----------------------------------------------------------------------------
  837. * Driver initialization and probing
  838. */
  839. static struct mt9v032_platform_data *
  840. mt9v032_get_pdata(struct i2c_client *client)
  841. {
  842. struct mt9v032_platform_data *pdata = NULL;
  843. struct v4l2_of_endpoint endpoint;
  844. struct device_node *np;
  845. struct property *prop;
  846. if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
  847. return client->dev.platform_data;
  848. np = of_graph_get_next_endpoint(client->dev.of_node, NULL);
  849. if (!np)
  850. return NULL;
  851. if (v4l2_of_parse_endpoint(np, &endpoint) < 0)
  852. goto done;
  853. pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
  854. if (!pdata)
  855. goto done;
  856. prop = of_find_property(np, "link-frequencies", NULL);
  857. if (prop) {
  858. u64 *link_freqs;
  859. size_t size = prop->length / sizeof(*link_freqs);
  860. link_freqs = devm_kcalloc(&client->dev, size,
  861. sizeof(*link_freqs), GFP_KERNEL);
  862. if (!link_freqs)
  863. goto done;
  864. if (of_property_read_u64_array(np, "link-frequencies",
  865. link_freqs, size) < 0)
  866. goto done;
  867. pdata->link_freqs = link_freqs;
  868. pdata->link_def_freq = link_freqs[0];
  869. }
  870. pdata->clk_pol = !!(endpoint.bus.parallel.flags &
  871. V4L2_MBUS_PCLK_SAMPLE_RISING);
  872. done:
  873. of_node_put(np);
  874. return pdata;
  875. }
  876. static int mt9v032_probe(struct i2c_client *client,
  877. const struct i2c_device_id *did)
  878. {
  879. struct mt9v032_platform_data *pdata = mt9v032_get_pdata(client);
  880. struct mt9v032 *mt9v032;
  881. unsigned int i;
  882. int ret;
  883. mt9v032 = devm_kzalloc(&client->dev, sizeof(*mt9v032), GFP_KERNEL);
  884. if (!mt9v032)
  885. return -ENOMEM;
  886. mt9v032->regmap = devm_regmap_init_i2c(client, &mt9v032_regmap_config);
  887. if (IS_ERR(mt9v032->regmap))
  888. return PTR_ERR(mt9v032->regmap);
  889. mt9v032->clk = devm_clk_get(&client->dev, NULL);
  890. if (IS_ERR(mt9v032->clk))
  891. return PTR_ERR(mt9v032->clk);
  892. mt9v032->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
  893. GPIOD_OUT_HIGH);
  894. if (IS_ERR(mt9v032->reset_gpio))
  895. return PTR_ERR(mt9v032->reset_gpio);
  896. mt9v032->standby_gpio = devm_gpiod_get_optional(&client->dev, "standby",
  897. GPIOD_OUT_LOW);
  898. if (IS_ERR(mt9v032->standby_gpio))
  899. return PTR_ERR(mt9v032->standby_gpio);
  900. mutex_init(&mt9v032->power_lock);
  901. mt9v032->pdata = pdata;
  902. mt9v032->model = (const void *)did->driver_data;
  903. v4l2_ctrl_handler_init(&mt9v032->ctrls, 11 +
  904. ARRAY_SIZE(mt9v032_aegc_controls));
  905. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  906. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  907. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  908. V4L2_CID_GAIN, MT9V032_ANALOG_GAIN_MIN,
  909. MT9V032_ANALOG_GAIN_MAX, 1, MT9V032_ANALOG_GAIN_DEF);
  910. v4l2_ctrl_new_std_menu(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  911. V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL, 0,
  912. V4L2_EXPOSURE_AUTO);
  913. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  914. V4L2_CID_EXPOSURE, mt9v032->model->data->min_shutter,
  915. mt9v032->model->data->max_shutter, 1,
  916. MT9V032_TOTAL_SHUTTER_WIDTH_DEF);
  917. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  918. V4L2_CID_HBLANK, mt9v032->model->data->min_hblank,
  919. MT9V032_HORIZONTAL_BLANKING_MAX, 1,
  920. MT9V032_HORIZONTAL_BLANKING_DEF);
  921. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  922. V4L2_CID_VBLANK, mt9v032->model->data->min_vblank,
  923. mt9v032->model->data->max_vblank, 1,
  924. MT9V032_VERTICAL_BLANKING_DEF);
  925. mt9v032->test_pattern = v4l2_ctrl_new_std_menu_items(&mt9v032->ctrls,
  926. &mt9v032_ctrl_ops, V4L2_CID_TEST_PATTERN,
  927. ARRAY_SIZE(mt9v032_test_pattern_menu) - 1, 0, 0,
  928. mt9v032_test_pattern_menu);
  929. mt9v032->test_pattern_color = v4l2_ctrl_new_custom(&mt9v032->ctrls,
  930. &mt9v032_test_pattern_color, NULL);
  931. v4l2_ctrl_new_custom(&mt9v032->ctrls,
  932. mt9v032->model->data->aec_max_shutter_v4l2_ctrl,
  933. NULL);
  934. for (i = 0; i < ARRAY_SIZE(mt9v032_aegc_controls); ++i)
  935. v4l2_ctrl_new_custom(&mt9v032->ctrls, &mt9v032_aegc_controls[i],
  936. NULL);
  937. v4l2_ctrl_cluster(2, &mt9v032->test_pattern);
  938. mt9v032->pixel_rate =
  939. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  940. V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
  941. if (pdata && pdata->link_freqs) {
  942. unsigned int def = 0;
  943. for (i = 0; pdata->link_freqs[i]; ++i) {
  944. if (pdata->link_freqs[i] == pdata->link_def_freq)
  945. def = i;
  946. }
  947. mt9v032->link_freq =
  948. v4l2_ctrl_new_int_menu(&mt9v032->ctrls,
  949. &mt9v032_ctrl_ops,
  950. V4L2_CID_LINK_FREQ, i - 1, def,
  951. pdata->link_freqs);
  952. v4l2_ctrl_cluster(2, &mt9v032->link_freq);
  953. }
  954. mt9v032->subdev.ctrl_handler = &mt9v032->ctrls;
  955. if (mt9v032->ctrls.error) {
  956. dev_err(&client->dev, "control initialization error %d\n",
  957. mt9v032->ctrls.error);
  958. ret = mt9v032->ctrls.error;
  959. goto err;
  960. }
  961. mt9v032->crop.left = MT9V032_COLUMN_START_DEF;
  962. mt9v032->crop.top = MT9V032_ROW_START_DEF;
  963. mt9v032->crop.width = MT9V032_WINDOW_WIDTH_DEF;
  964. mt9v032->crop.height = MT9V032_WINDOW_HEIGHT_DEF;
  965. if (mt9v032->model->color)
  966. mt9v032->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
  967. else
  968. mt9v032->format.code = MEDIA_BUS_FMT_Y10_1X10;
  969. mt9v032->format.width = MT9V032_WINDOW_WIDTH_DEF;
  970. mt9v032->format.height = MT9V032_WINDOW_HEIGHT_DEF;
  971. mt9v032->format.field = V4L2_FIELD_NONE;
  972. mt9v032->format.colorspace = V4L2_COLORSPACE_SRGB;
  973. mt9v032->hratio = 1;
  974. mt9v032->vratio = 1;
  975. mt9v032->aec_agc = MT9V032_AEC_ENABLE | MT9V032_AGC_ENABLE;
  976. mt9v032->hblank = MT9V032_HORIZONTAL_BLANKING_DEF;
  977. mt9v032->sysclk = MT9V032_SYSCLK_FREQ_DEF;
  978. v4l2_i2c_subdev_init(&mt9v032->subdev, client, &mt9v032_subdev_ops);
  979. mt9v032->subdev.internal_ops = &mt9v032_subdev_internal_ops;
  980. mt9v032->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  981. mt9v032->pad.flags = MEDIA_PAD_FL_SOURCE;
  982. ret = media_entity_pads_init(&mt9v032->subdev.entity, 1, &mt9v032->pad);
  983. if (ret < 0)
  984. goto err;
  985. mt9v032->subdev.dev = &client->dev;
  986. ret = v4l2_async_register_subdev(&mt9v032->subdev);
  987. if (ret < 0)
  988. goto err;
  989. return 0;
  990. err:
  991. media_entity_cleanup(&mt9v032->subdev.entity);
  992. v4l2_ctrl_handler_free(&mt9v032->ctrls);
  993. return ret;
  994. }
  995. static int mt9v032_remove(struct i2c_client *client)
  996. {
  997. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  998. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  999. v4l2_async_unregister_subdev(subdev);
  1000. v4l2_ctrl_handler_free(&mt9v032->ctrls);
  1001. media_entity_cleanup(&subdev->entity);
  1002. return 0;
  1003. }
  1004. static const struct mt9v032_model_data mt9v032_model_data[] = {
  1005. {
  1006. /* MT9V022, MT9V032 revisions 1/2/3 */
  1007. .min_row_time = 660,
  1008. .min_hblank = MT9V032_HORIZONTAL_BLANKING_MIN,
  1009. .min_vblank = MT9V032_VERTICAL_BLANKING_MIN,
  1010. .max_vblank = MT9V032_VERTICAL_BLANKING_MAX,
  1011. .min_shutter = MT9V032_TOTAL_SHUTTER_WIDTH_MIN,
  1012. .max_shutter = MT9V032_TOTAL_SHUTTER_WIDTH_MAX,
  1013. .pclk_reg = MT9V032_PIXEL_CLOCK,
  1014. .aec_max_shutter_reg = MT9V032_AEC_MAX_SHUTTER_WIDTH,
  1015. .aec_max_shutter_v4l2_ctrl = &mt9v032_aec_max_shutter_width,
  1016. }, {
  1017. /* MT9V024, MT9V034 */
  1018. .min_row_time = 690,
  1019. .min_hblank = MT9V034_HORIZONTAL_BLANKING_MIN,
  1020. .min_vblank = MT9V034_VERTICAL_BLANKING_MIN,
  1021. .max_vblank = MT9V034_VERTICAL_BLANKING_MAX,
  1022. .min_shutter = MT9V034_TOTAL_SHUTTER_WIDTH_MIN,
  1023. .max_shutter = MT9V034_TOTAL_SHUTTER_WIDTH_MAX,
  1024. .pclk_reg = MT9V034_PIXEL_CLOCK,
  1025. .aec_max_shutter_reg = MT9V034_AEC_MAX_SHUTTER_WIDTH,
  1026. .aec_max_shutter_v4l2_ctrl = &mt9v034_aec_max_shutter_width,
  1027. },
  1028. };
  1029. static const struct mt9v032_model_info mt9v032_models[] = {
  1030. [MT9V032_MODEL_V022_COLOR] = {
  1031. .data = &mt9v032_model_data[0],
  1032. .color = true,
  1033. },
  1034. [MT9V032_MODEL_V022_MONO] = {
  1035. .data = &mt9v032_model_data[0],
  1036. .color = false,
  1037. },
  1038. [MT9V032_MODEL_V024_COLOR] = {
  1039. .data = &mt9v032_model_data[1],
  1040. .color = true,
  1041. },
  1042. [MT9V032_MODEL_V024_MONO] = {
  1043. .data = &mt9v032_model_data[1],
  1044. .color = false,
  1045. },
  1046. [MT9V032_MODEL_V032_COLOR] = {
  1047. .data = &mt9v032_model_data[0],
  1048. .color = true,
  1049. },
  1050. [MT9V032_MODEL_V032_MONO] = {
  1051. .data = &mt9v032_model_data[0],
  1052. .color = false,
  1053. },
  1054. [MT9V032_MODEL_V034_COLOR] = {
  1055. .data = &mt9v032_model_data[1],
  1056. .color = true,
  1057. },
  1058. [MT9V032_MODEL_V034_MONO] = {
  1059. .data = &mt9v032_model_data[1],
  1060. .color = false,
  1061. },
  1062. };
  1063. static const struct i2c_device_id mt9v032_id[] = {
  1064. { "mt9v022", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V022_COLOR] },
  1065. { "mt9v022m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V022_MONO] },
  1066. { "mt9v024", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V024_COLOR] },
  1067. { "mt9v024m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V024_MONO] },
  1068. { "mt9v032", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V032_COLOR] },
  1069. { "mt9v032m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V032_MONO] },
  1070. { "mt9v034", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V034_COLOR] },
  1071. { "mt9v034m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V034_MONO] },
  1072. { }
  1073. };
  1074. MODULE_DEVICE_TABLE(i2c, mt9v032_id);
  1075. #if IS_ENABLED(CONFIG_OF)
  1076. static const struct of_device_id mt9v032_of_match[] = {
  1077. { .compatible = "aptina,mt9v022" },
  1078. { .compatible = "aptina,mt9v022m" },
  1079. { .compatible = "aptina,mt9v024" },
  1080. { .compatible = "aptina,mt9v024m" },
  1081. { .compatible = "aptina,mt9v032" },
  1082. { .compatible = "aptina,mt9v032m" },
  1083. { .compatible = "aptina,mt9v034" },
  1084. { .compatible = "aptina,mt9v034m" },
  1085. { /* Sentinel */ }
  1086. };
  1087. MODULE_DEVICE_TABLE(of, mt9v032_of_match);
  1088. #endif
  1089. static struct i2c_driver mt9v032_driver = {
  1090. .driver = {
  1091. .name = "mt9v032",
  1092. .of_match_table = of_match_ptr(mt9v032_of_match),
  1093. },
  1094. .probe = mt9v032_probe,
  1095. .remove = mt9v032_remove,
  1096. .id_table = mt9v032_id,
  1097. };
  1098. module_i2c_driver(mt9v032_driver);
  1099. MODULE_DESCRIPTION("Aptina MT9V032 Camera driver");
  1100. MODULE_AUTHOR("Laurent Pinchart <[email protected]>");
  1101. MODULE_LICENSE("GPL");