mt9v032.c 36 KB

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