mt9v032.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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. if (code->index > 0)
  378. return -EINVAL;
  379. code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
  380. return 0;
  381. }
  382. static int mt9v032_enum_frame_size(struct v4l2_subdev *subdev,
  383. struct v4l2_subdev_pad_config *cfg,
  384. struct v4l2_subdev_frame_size_enum *fse)
  385. {
  386. if (fse->index >= 3 || fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
  387. return -EINVAL;
  388. fse->min_width = MT9V032_WINDOW_WIDTH_DEF / (1 << fse->index);
  389. fse->max_width = fse->min_width;
  390. fse->min_height = MT9V032_WINDOW_HEIGHT_DEF / (1 << fse->index);
  391. fse->max_height = fse->min_height;
  392. return 0;
  393. }
  394. static int mt9v032_get_format(struct v4l2_subdev *subdev,
  395. struct v4l2_subdev_pad_config *cfg,
  396. struct v4l2_subdev_format *format)
  397. {
  398. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  399. format->format = *__mt9v032_get_pad_format(mt9v032, cfg, format->pad,
  400. format->which);
  401. return 0;
  402. }
  403. static void mt9v032_configure_pixel_rate(struct mt9v032 *mt9v032)
  404. {
  405. struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
  406. int ret;
  407. ret = v4l2_ctrl_s_ctrl_int64(mt9v032->pixel_rate,
  408. mt9v032->sysclk / mt9v032->hratio);
  409. if (ret < 0)
  410. dev_warn(&client->dev, "failed to set pixel rate (%d)\n", ret);
  411. }
  412. static unsigned int mt9v032_calc_ratio(unsigned int input, unsigned int output)
  413. {
  414. /* Compute the power-of-two binning factor closest to the input size to
  415. * output size ratio. Given that the output size is bounded by input/4
  416. * and input, a generic implementation would be an ineffective luxury.
  417. */
  418. if (output * 3 > input * 2)
  419. return 1;
  420. if (output * 3 > input)
  421. return 2;
  422. return 4;
  423. }
  424. static int mt9v032_set_format(struct v4l2_subdev *subdev,
  425. struct v4l2_subdev_pad_config *cfg,
  426. struct v4l2_subdev_format *format)
  427. {
  428. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  429. struct v4l2_mbus_framefmt *__format;
  430. struct v4l2_rect *__crop;
  431. unsigned int width;
  432. unsigned int height;
  433. unsigned int hratio;
  434. unsigned int vratio;
  435. __crop = __mt9v032_get_pad_crop(mt9v032, cfg, format->pad,
  436. format->which);
  437. /* Clamp the width and height to avoid dividing by zero. */
  438. width = clamp(ALIGN(format->format.width, 2),
  439. max_t(unsigned int, __crop->width / 4,
  440. MT9V032_WINDOW_WIDTH_MIN),
  441. __crop->width);
  442. height = clamp(ALIGN(format->format.height, 2),
  443. max_t(unsigned int, __crop->height / 4,
  444. MT9V032_WINDOW_HEIGHT_MIN),
  445. __crop->height);
  446. hratio = mt9v032_calc_ratio(__crop->width, width);
  447. vratio = mt9v032_calc_ratio(__crop->height, height);
  448. __format = __mt9v032_get_pad_format(mt9v032, cfg, format->pad,
  449. format->which);
  450. __format->width = __crop->width / hratio;
  451. __format->height = __crop->height / vratio;
  452. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  453. mt9v032->hratio = hratio;
  454. mt9v032->vratio = vratio;
  455. mt9v032_configure_pixel_rate(mt9v032);
  456. }
  457. format->format = *__format;
  458. return 0;
  459. }
  460. static int mt9v032_get_selection(struct v4l2_subdev *subdev,
  461. struct v4l2_subdev_pad_config *cfg,
  462. struct v4l2_subdev_selection *sel)
  463. {
  464. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  465. if (sel->target != V4L2_SEL_TGT_CROP)
  466. return -EINVAL;
  467. sel->r = *__mt9v032_get_pad_crop(mt9v032, cfg, sel->pad, sel->which);
  468. return 0;
  469. }
  470. static int mt9v032_set_selection(struct v4l2_subdev *subdev,
  471. struct v4l2_subdev_pad_config *cfg,
  472. struct v4l2_subdev_selection *sel)
  473. {
  474. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  475. struct v4l2_mbus_framefmt *__format;
  476. struct v4l2_rect *__crop;
  477. struct v4l2_rect rect;
  478. if (sel->target != V4L2_SEL_TGT_CROP)
  479. return -EINVAL;
  480. /* Clamp the crop rectangle boundaries and align them to a non multiple
  481. * of 2 pixels to ensure a GRBG Bayer pattern.
  482. */
  483. rect.left = clamp(ALIGN(sel->r.left + 1, 2) - 1,
  484. MT9V032_COLUMN_START_MIN,
  485. MT9V032_COLUMN_START_MAX);
  486. rect.top = clamp(ALIGN(sel->r.top + 1, 2) - 1,
  487. MT9V032_ROW_START_MIN,
  488. MT9V032_ROW_START_MAX);
  489. rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
  490. MT9V032_WINDOW_WIDTH_MIN,
  491. MT9V032_WINDOW_WIDTH_MAX);
  492. rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
  493. MT9V032_WINDOW_HEIGHT_MIN,
  494. MT9V032_WINDOW_HEIGHT_MAX);
  495. rect.width = min_t(unsigned int,
  496. rect.width, MT9V032_PIXEL_ARRAY_WIDTH - rect.left);
  497. rect.height = min_t(unsigned int,
  498. rect.height, MT9V032_PIXEL_ARRAY_HEIGHT - rect.top);
  499. __crop = __mt9v032_get_pad_crop(mt9v032, cfg, sel->pad, sel->which);
  500. if (rect.width != __crop->width || rect.height != __crop->height) {
  501. /* Reset the output image size if the crop rectangle size has
  502. * been modified.
  503. */
  504. __format = __mt9v032_get_pad_format(mt9v032, cfg, sel->pad,
  505. sel->which);
  506. __format->width = rect.width;
  507. __format->height = rect.height;
  508. if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  509. mt9v032->hratio = 1;
  510. mt9v032->vratio = 1;
  511. mt9v032_configure_pixel_rate(mt9v032);
  512. }
  513. }
  514. *__crop = rect;
  515. sel->r = rect;
  516. return 0;
  517. }
  518. /* -----------------------------------------------------------------------------
  519. * V4L2 subdev control operations
  520. */
  521. #define V4L2_CID_TEST_PATTERN_COLOR (V4L2_CID_USER_BASE | 0x1001)
  522. /*
  523. * Value between 1 and 64 to set the desired bin. This is effectively a measure
  524. * of how bright the image is supposed to be. Both AGC and AEC try to reach
  525. * this.
  526. */
  527. #define V4L2_CID_AEGC_DESIRED_BIN (V4L2_CID_USER_BASE | 0x1002)
  528. /*
  529. * LPF is the low pass filter capability of the chip. Both AEC and AGC have
  530. * this setting. This limits the speed in which AGC/AEC adjust their settings.
  531. * Possible values are 0-2. 0 means no LPF. For 1 and 2 this equation is used:
  532. *
  533. * if |(calculated new exp - current exp)| > (current exp / 4)
  534. * next exp = calculated new exp
  535. * else
  536. * next exp = current exp + ((calculated new exp - current exp) / 2^LPF)
  537. */
  538. #define V4L2_CID_AEC_LPF (V4L2_CID_USER_BASE | 0x1003)
  539. #define V4L2_CID_AGC_LPF (V4L2_CID_USER_BASE | 0x1004)
  540. /*
  541. * Value between 0 and 15. This is the number of frames being skipped before
  542. * updating the auto exposure/gain.
  543. */
  544. #define V4L2_CID_AEC_UPDATE_INTERVAL (V4L2_CID_USER_BASE | 0x1005)
  545. #define V4L2_CID_AGC_UPDATE_INTERVAL (V4L2_CID_USER_BASE | 0x1006)
  546. /*
  547. * Maximum shutter width used for AEC.
  548. */
  549. #define V4L2_CID_AEC_MAX_SHUTTER_WIDTH (V4L2_CID_USER_BASE | 0x1007)
  550. static int mt9v032_s_ctrl(struct v4l2_ctrl *ctrl)
  551. {
  552. struct mt9v032 *mt9v032 =
  553. container_of(ctrl->handler, struct mt9v032, ctrls);
  554. struct regmap *map = mt9v032->regmap;
  555. u32 freq;
  556. u16 data;
  557. switch (ctrl->id) {
  558. case V4L2_CID_AUTOGAIN:
  559. return mt9v032_update_aec_agc(mt9v032, MT9V032_AGC_ENABLE,
  560. ctrl->val);
  561. case V4L2_CID_GAIN:
  562. return regmap_write(map, MT9V032_ANALOG_GAIN, ctrl->val);
  563. case V4L2_CID_EXPOSURE_AUTO:
  564. return mt9v032_update_aec_agc(mt9v032, MT9V032_AEC_ENABLE,
  565. !ctrl->val);
  566. case V4L2_CID_EXPOSURE:
  567. return regmap_write(map, MT9V032_TOTAL_SHUTTER_WIDTH,
  568. ctrl->val);
  569. case V4L2_CID_HBLANK:
  570. mt9v032->hblank = ctrl->val;
  571. return mt9v032_update_hblank(mt9v032);
  572. case V4L2_CID_VBLANK:
  573. return regmap_write(map, MT9V032_VERTICAL_BLANKING,
  574. ctrl->val);
  575. case V4L2_CID_PIXEL_RATE:
  576. case V4L2_CID_LINK_FREQ:
  577. if (mt9v032->link_freq == NULL)
  578. break;
  579. freq = mt9v032->pdata->link_freqs[mt9v032->link_freq->val];
  580. *mt9v032->pixel_rate->p_new.p_s64 = freq;
  581. mt9v032->sysclk = freq;
  582. break;
  583. case V4L2_CID_TEST_PATTERN:
  584. switch (mt9v032->test_pattern->val) {
  585. case 0:
  586. data = 0;
  587. break;
  588. case 1:
  589. data = MT9V032_TEST_PATTERN_GRAY_VERTICAL
  590. | MT9V032_TEST_PATTERN_ENABLE;
  591. break;
  592. case 2:
  593. data = MT9V032_TEST_PATTERN_GRAY_HORIZONTAL
  594. | MT9V032_TEST_PATTERN_ENABLE;
  595. break;
  596. case 3:
  597. data = MT9V032_TEST_PATTERN_GRAY_DIAGONAL
  598. | MT9V032_TEST_PATTERN_ENABLE;
  599. break;
  600. default:
  601. data = (mt9v032->test_pattern_color->val <<
  602. MT9V032_TEST_PATTERN_DATA_SHIFT)
  603. | MT9V032_TEST_PATTERN_USE_DATA
  604. | MT9V032_TEST_PATTERN_ENABLE
  605. | MT9V032_TEST_PATTERN_FLIP;
  606. break;
  607. }
  608. return regmap_write(map, MT9V032_TEST_PATTERN, data);
  609. case V4L2_CID_AEGC_DESIRED_BIN:
  610. return regmap_write(map, MT9V032_AEGC_DESIRED_BIN, ctrl->val);
  611. case V4L2_CID_AEC_LPF:
  612. return regmap_write(map, MT9V032_AEC_LPF, ctrl->val);
  613. case V4L2_CID_AGC_LPF:
  614. return regmap_write(map, MT9V032_AGC_LPF, ctrl->val);
  615. case V4L2_CID_AEC_UPDATE_INTERVAL:
  616. return regmap_write(map, MT9V032_AEC_UPDATE_FREQUENCY,
  617. ctrl->val);
  618. case V4L2_CID_AGC_UPDATE_INTERVAL:
  619. return regmap_write(map, MT9V032_AGC_UPDATE_FREQUENCY,
  620. ctrl->val);
  621. case V4L2_CID_AEC_MAX_SHUTTER_WIDTH:
  622. return regmap_write(map,
  623. mt9v032->model->data->aec_max_shutter_reg,
  624. ctrl->val);
  625. }
  626. return 0;
  627. }
  628. static const struct v4l2_ctrl_ops mt9v032_ctrl_ops = {
  629. .s_ctrl = mt9v032_s_ctrl,
  630. };
  631. static const char * const mt9v032_test_pattern_menu[] = {
  632. "Disabled",
  633. "Gray Vertical Shade",
  634. "Gray Horizontal Shade",
  635. "Gray Diagonal Shade",
  636. "Plain",
  637. };
  638. static const struct v4l2_ctrl_config mt9v032_test_pattern_color = {
  639. .ops = &mt9v032_ctrl_ops,
  640. .id = V4L2_CID_TEST_PATTERN_COLOR,
  641. .type = V4L2_CTRL_TYPE_INTEGER,
  642. .name = "Test Pattern Color",
  643. .min = 0,
  644. .max = 1023,
  645. .step = 1,
  646. .def = 0,
  647. .flags = 0,
  648. };
  649. static const struct v4l2_ctrl_config mt9v032_aegc_controls[] = {
  650. {
  651. .ops = &mt9v032_ctrl_ops,
  652. .id = V4L2_CID_AEGC_DESIRED_BIN,
  653. .type = V4L2_CTRL_TYPE_INTEGER,
  654. .name = "AEC/AGC Desired Bin",
  655. .min = 1,
  656. .max = 64,
  657. .step = 1,
  658. .def = 58,
  659. .flags = 0,
  660. }, {
  661. .ops = &mt9v032_ctrl_ops,
  662. .id = V4L2_CID_AEC_LPF,
  663. .type = V4L2_CTRL_TYPE_INTEGER,
  664. .name = "AEC Low Pass Filter",
  665. .min = 0,
  666. .max = 2,
  667. .step = 1,
  668. .def = 0,
  669. .flags = 0,
  670. }, {
  671. .ops = &mt9v032_ctrl_ops,
  672. .id = V4L2_CID_AGC_LPF,
  673. .type = V4L2_CTRL_TYPE_INTEGER,
  674. .name = "AGC Low Pass Filter",
  675. .min = 0,
  676. .max = 2,
  677. .step = 1,
  678. .def = 2,
  679. .flags = 0,
  680. }, {
  681. .ops = &mt9v032_ctrl_ops,
  682. .id = V4L2_CID_AEC_UPDATE_INTERVAL,
  683. .type = V4L2_CTRL_TYPE_INTEGER,
  684. .name = "AEC Update Interval",
  685. .min = 0,
  686. .max = 16,
  687. .step = 1,
  688. .def = 2,
  689. .flags = 0,
  690. }, {
  691. .ops = &mt9v032_ctrl_ops,
  692. .id = V4L2_CID_AGC_UPDATE_INTERVAL,
  693. .type = V4L2_CTRL_TYPE_INTEGER,
  694. .name = "AGC Update Interval",
  695. .min = 0,
  696. .max = 16,
  697. .step = 1,
  698. .def = 2,
  699. .flags = 0,
  700. }
  701. };
  702. static const struct v4l2_ctrl_config mt9v032_aec_max_shutter_width = {
  703. .ops = &mt9v032_ctrl_ops,
  704. .id = V4L2_CID_AEC_MAX_SHUTTER_WIDTH,
  705. .type = V4L2_CTRL_TYPE_INTEGER,
  706. .name = "AEC Max Shutter Width",
  707. .min = 1,
  708. .max = 2047,
  709. .step = 1,
  710. .def = 480,
  711. .flags = 0,
  712. };
  713. static const struct v4l2_ctrl_config mt9v034_aec_max_shutter_width = {
  714. .ops = &mt9v032_ctrl_ops,
  715. .id = V4L2_CID_AEC_MAX_SHUTTER_WIDTH,
  716. .type = V4L2_CTRL_TYPE_INTEGER,
  717. .name = "AEC Max Shutter Width",
  718. .min = 1,
  719. .max = 32765,
  720. .step = 1,
  721. .def = 480,
  722. .flags = 0,
  723. };
  724. /* -----------------------------------------------------------------------------
  725. * V4L2 subdev core operations
  726. */
  727. static int mt9v032_set_power(struct v4l2_subdev *subdev, int on)
  728. {
  729. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  730. int ret = 0;
  731. mutex_lock(&mt9v032->power_lock);
  732. /* If the power count is modified from 0 to != 0 or from != 0 to 0,
  733. * update the power state.
  734. */
  735. if (mt9v032->power_count == !on) {
  736. ret = __mt9v032_set_power(mt9v032, !!on);
  737. if (ret < 0)
  738. goto done;
  739. }
  740. /* Update the power count. */
  741. mt9v032->power_count += on ? 1 : -1;
  742. WARN_ON(mt9v032->power_count < 0);
  743. done:
  744. mutex_unlock(&mt9v032->power_lock);
  745. return ret;
  746. }
  747. /* -----------------------------------------------------------------------------
  748. * V4L2 subdev internal operations
  749. */
  750. static int mt9v032_registered(struct v4l2_subdev *subdev)
  751. {
  752. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  753. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  754. unsigned int i;
  755. u32 version;
  756. int ret;
  757. dev_info(&client->dev, "Probing MT9V032 at address 0x%02x\n",
  758. client->addr);
  759. ret = mt9v032_power_on(mt9v032);
  760. if (ret < 0) {
  761. dev_err(&client->dev, "MT9V032 power up failed\n");
  762. return ret;
  763. }
  764. /* Read and check the sensor version */
  765. ret = regmap_read(mt9v032->regmap, MT9V032_CHIP_VERSION, &version);
  766. mt9v032_power_off(mt9v032);
  767. if (ret < 0) {
  768. dev_err(&client->dev, "Failed reading chip version\n");
  769. return ret;
  770. }
  771. for (i = 0; i < ARRAY_SIZE(mt9v032_versions); ++i) {
  772. if (mt9v032_versions[i].version == version) {
  773. mt9v032->version = &mt9v032_versions[i];
  774. break;
  775. }
  776. }
  777. if (mt9v032->version == NULL) {
  778. dev_err(&client->dev, "Unsupported chip version 0x%04x\n",
  779. version);
  780. return -ENODEV;
  781. }
  782. dev_info(&client->dev, "%s detected at address 0x%02x\n",
  783. mt9v032->version->name, client->addr);
  784. mt9v032_configure_pixel_rate(mt9v032);
  785. return ret;
  786. }
  787. static int mt9v032_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
  788. {
  789. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  790. struct v4l2_mbus_framefmt *format;
  791. struct v4l2_rect *crop;
  792. crop = v4l2_subdev_get_try_crop(subdev, fh->pad, 0);
  793. crop->left = MT9V032_COLUMN_START_DEF;
  794. crop->top = MT9V032_ROW_START_DEF;
  795. crop->width = MT9V032_WINDOW_WIDTH_DEF;
  796. crop->height = MT9V032_WINDOW_HEIGHT_DEF;
  797. format = v4l2_subdev_get_try_format(subdev, fh->pad, 0);
  798. if (mt9v032->model->color)
  799. format->code = MEDIA_BUS_FMT_SGRBG10_1X10;
  800. else
  801. format->code = MEDIA_BUS_FMT_Y10_1X10;
  802. format->width = MT9V032_WINDOW_WIDTH_DEF;
  803. format->height = MT9V032_WINDOW_HEIGHT_DEF;
  804. format->field = V4L2_FIELD_NONE;
  805. format->colorspace = V4L2_COLORSPACE_SRGB;
  806. return mt9v032_set_power(subdev, 1);
  807. }
  808. static int mt9v032_close(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
  809. {
  810. return mt9v032_set_power(subdev, 0);
  811. }
  812. static const struct v4l2_subdev_core_ops mt9v032_subdev_core_ops = {
  813. .s_power = mt9v032_set_power,
  814. };
  815. static const struct v4l2_subdev_video_ops mt9v032_subdev_video_ops = {
  816. .s_stream = mt9v032_s_stream,
  817. };
  818. static const struct v4l2_subdev_pad_ops mt9v032_subdev_pad_ops = {
  819. .enum_mbus_code = mt9v032_enum_mbus_code,
  820. .enum_frame_size = mt9v032_enum_frame_size,
  821. .get_fmt = mt9v032_get_format,
  822. .set_fmt = mt9v032_set_format,
  823. .get_selection = mt9v032_get_selection,
  824. .set_selection = mt9v032_set_selection,
  825. };
  826. static const struct v4l2_subdev_ops mt9v032_subdev_ops = {
  827. .core = &mt9v032_subdev_core_ops,
  828. .video = &mt9v032_subdev_video_ops,
  829. .pad = &mt9v032_subdev_pad_ops,
  830. };
  831. static const struct v4l2_subdev_internal_ops mt9v032_subdev_internal_ops = {
  832. .registered = mt9v032_registered,
  833. .open = mt9v032_open,
  834. .close = mt9v032_close,
  835. };
  836. static const struct regmap_config mt9v032_regmap_config = {
  837. .reg_bits = 8,
  838. .val_bits = 16,
  839. .max_register = 0xff,
  840. .cache_type = REGCACHE_RBTREE,
  841. };
  842. /* -----------------------------------------------------------------------------
  843. * Driver initialization and probing
  844. */
  845. static struct mt9v032_platform_data *
  846. mt9v032_get_pdata(struct i2c_client *client)
  847. {
  848. struct mt9v032_platform_data *pdata = NULL;
  849. struct v4l2_fwnode_endpoint endpoint;
  850. struct device_node *np;
  851. struct property *prop;
  852. if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
  853. return client->dev.platform_data;
  854. np = of_graph_get_next_endpoint(client->dev.of_node, NULL);
  855. if (!np)
  856. return NULL;
  857. if (v4l2_fwnode_endpoint_parse(of_fwnode_handle(np), &endpoint) < 0)
  858. goto done;
  859. pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
  860. if (!pdata)
  861. goto done;
  862. prop = of_find_property(np, "link-frequencies", NULL);
  863. if (prop) {
  864. u64 *link_freqs;
  865. size_t size = prop->length / sizeof(*link_freqs);
  866. link_freqs = devm_kcalloc(&client->dev, size,
  867. sizeof(*link_freqs), GFP_KERNEL);
  868. if (!link_freqs)
  869. goto done;
  870. if (of_property_read_u64_array(np, "link-frequencies",
  871. link_freqs, size) < 0)
  872. goto done;
  873. pdata->link_freqs = link_freqs;
  874. pdata->link_def_freq = link_freqs[0];
  875. }
  876. pdata->clk_pol = !!(endpoint.bus.parallel.flags &
  877. V4L2_MBUS_PCLK_SAMPLE_RISING);
  878. done:
  879. of_node_put(np);
  880. return pdata;
  881. }
  882. static int mt9v032_probe(struct i2c_client *client,
  883. const struct i2c_device_id *did)
  884. {
  885. struct mt9v032_platform_data *pdata = mt9v032_get_pdata(client);
  886. struct mt9v032 *mt9v032;
  887. unsigned int i;
  888. int ret;
  889. mt9v032 = devm_kzalloc(&client->dev, sizeof(*mt9v032), GFP_KERNEL);
  890. if (!mt9v032)
  891. return -ENOMEM;
  892. mt9v032->regmap = devm_regmap_init_i2c(client, &mt9v032_regmap_config);
  893. if (IS_ERR(mt9v032->regmap))
  894. return PTR_ERR(mt9v032->regmap);
  895. mt9v032->clk = devm_clk_get(&client->dev, NULL);
  896. if (IS_ERR(mt9v032->clk))
  897. return PTR_ERR(mt9v032->clk);
  898. mt9v032->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
  899. GPIOD_OUT_HIGH);
  900. if (IS_ERR(mt9v032->reset_gpio))
  901. return PTR_ERR(mt9v032->reset_gpio);
  902. mt9v032->standby_gpio = devm_gpiod_get_optional(&client->dev, "standby",
  903. GPIOD_OUT_LOW);
  904. if (IS_ERR(mt9v032->standby_gpio))
  905. return PTR_ERR(mt9v032->standby_gpio);
  906. mutex_init(&mt9v032->power_lock);
  907. mt9v032->pdata = pdata;
  908. mt9v032->model = (const void *)did->driver_data;
  909. v4l2_ctrl_handler_init(&mt9v032->ctrls, 11 +
  910. ARRAY_SIZE(mt9v032_aegc_controls));
  911. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  912. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  913. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  914. V4L2_CID_GAIN, MT9V032_ANALOG_GAIN_MIN,
  915. MT9V032_ANALOG_GAIN_MAX, 1, MT9V032_ANALOG_GAIN_DEF);
  916. v4l2_ctrl_new_std_menu(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  917. V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL, 0,
  918. V4L2_EXPOSURE_AUTO);
  919. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  920. V4L2_CID_EXPOSURE, mt9v032->model->data->min_shutter,
  921. mt9v032->model->data->max_shutter, 1,
  922. MT9V032_TOTAL_SHUTTER_WIDTH_DEF);
  923. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  924. V4L2_CID_HBLANK, mt9v032->model->data->min_hblank,
  925. MT9V032_HORIZONTAL_BLANKING_MAX, 1,
  926. MT9V032_HORIZONTAL_BLANKING_DEF);
  927. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  928. V4L2_CID_VBLANK, mt9v032->model->data->min_vblank,
  929. mt9v032->model->data->max_vblank, 1,
  930. MT9V032_VERTICAL_BLANKING_DEF);
  931. mt9v032->test_pattern = v4l2_ctrl_new_std_menu_items(&mt9v032->ctrls,
  932. &mt9v032_ctrl_ops, V4L2_CID_TEST_PATTERN,
  933. ARRAY_SIZE(mt9v032_test_pattern_menu) - 1, 0, 0,
  934. mt9v032_test_pattern_menu);
  935. mt9v032->test_pattern_color = v4l2_ctrl_new_custom(&mt9v032->ctrls,
  936. &mt9v032_test_pattern_color, NULL);
  937. v4l2_ctrl_new_custom(&mt9v032->ctrls,
  938. mt9v032->model->data->aec_max_shutter_v4l2_ctrl,
  939. NULL);
  940. for (i = 0; i < ARRAY_SIZE(mt9v032_aegc_controls); ++i)
  941. v4l2_ctrl_new_custom(&mt9v032->ctrls, &mt9v032_aegc_controls[i],
  942. NULL);
  943. v4l2_ctrl_cluster(2, &mt9v032->test_pattern);
  944. mt9v032->pixel_rate =
  945. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  946. V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
  947. if (pdata && pdata->link_freqs) {
  948. unsigned int def = 0;
  949. for (i = 0; pdata->link_freqs[i]; ++i) {
  950. if (pdata->link_freqs[i] == pdata->link_def_freq)
  951. def = i;
  952. }
  953. mt9v032->link_freq =
  954. v4l2_ctrl_new_int_menu(&mt9v032->ctrls,
  955. &mt9v032_ctrl_ops,
  956. V4L2_CID_LINK_FREQ, i - 1, def,
  957. pdata->link_freqs);
  958. v4l2_ctrl_cluster(2, &mt9v032->link_freq);
  959. }
  960. mt9v032->subdev.ctrl_handler = &mt9v032->ctrls;
  961. if (mt9v032->ctrls.error) {
  962. dev_err(&client->dev, "control initialization error %d\n",
  963. mt9v032->ctrls.error);
  964. ret = mt9v032->ctrls.error;
  965. goto err;
  966. }
  967. mt9v032->crop.left = MT9V032_COLUMN_START_DEF;
  968. mt9v032->crop.top = MT9V032_ROW_START_DEF;
  969. mt9v032->crop.width = MT9V032_WINDOW_WIDTH_DEF;
  970. mt9v032->crop.height = MT9V032_WINDOW_HEIGHT_DEF;
  971. if (mt9v032->model->color)
  972. mt9v032->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
  973. else
  974. mt9v032->format.code = MEDIA_BUS_FMT_Y10_1X10;
  975. mt9v032->format.width = MT9V032_WINDOW_WIDTH_DEF;
  976. mt9v032->format.height = MT9V032_WINDOW_HEIGHT_DEF;
  977. mt9v032->format.field = V4L2_FIELD_NONE;
  978. mt9v032->format.colorspace = V4L2_COLORSPACE_SRGB;
  979. mt9v032->hratio = 1;
  980. mt9v032->vratio = 1;
  981. mt9v032->aec_agc = MT9V032_AEC_ENABLE | MT9V032_AGC_ENABLE;
  982. mt9v032->hblank = MT9V032_HORIZONTAL_BLANKING_DEF;
  983. mt9v032->sysclk = MT9V032_SYSCLK_FREQ_DEF;
  984. v4l2_i2c_subdev_init(&mt9v032->subdev, client, &mt9v032_subdev_ops);
  985. mt9v032->subdev.internal_ops = &mt9v032_subdev_internal_ops;
  986. mt9v032->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  987. mt9v032->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  988. mt9v032->pad.flags = MEDIA_PAD_FL_SOURCE;
  989. ret = media_entity_pads_init(&mt9v032->subdev.entity, 1, &mt9v032->pad);
  990. if (ret < 0)
  991. goto err;
  992. mt9v032->subdev.dev = &client->dev;
  993. ret = v4l2_async_register_subdev(&mt9v032->subdev);
  994. if (ret < 0)
  995. goto err;
  996. return 0;
  997. err:
  998. media_entity_cleanup(&mt9v032->subdev.entity);
  999. v4l2_ctrl_handler_free(&mt9v032->ctrls);
  1000. return ret;
  1001. }
  1002. static int mt9v032_remove(struct i2c_client *client)
  1003. {
  1004. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  1005. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  1006. v4l2_async_unregister_subdev(subdev);
  1007. v4l2_ctrl_handler_free(&mt9v032->ctrls);
  1008. media_entity_cleanup(&subdev->entity);
  1009. return 0;
  1010. }
  1011. static const struct mt9v032_model_data mt9v032_model_data[] = {
  1012. {
  1013. /* MT9V022, MT9V032 revisions 1/2/3 */
  1014. .min_row_time = 660,
  1015. .min_hblank = MT9V032_HORIZONTAL_BLANKING_MIN,
  1016. .min_vblank = MT9V032_VERTICAL_BLANKING_MIN,
  1017. .max_vblank = MT9V032_VERTICAL_BLANKING_MAX,
  1018. .min_shutter = MT9V032_TOTAL_SHUTTER_WIDTH_MIN,
  1019. .max_shutter = MT9V032_TOTAL_SHUTTER_WIDTH_MAX,
  1020. .pclk_reg = MT9V032_PIXEL_CLOCK,
  1021. .aec_max_shutter_reg = MT9V032_AEC_MAX_SHUTTER_WIDTH,
  1022. .aec_max_shutter_v4l2_ctrl = &mt9v032_aec_max_shutter_width,
  1023. }, {
  1024. /* MT9V024, MT9V034 */
  1025. .min_row_time = 690,
  1026. .min_hblank = MT9V034_HORIZONTAL_BLANKING_MIN,
  1027. .min_vblank = MT9V034_VERTICAL_BLANKING_MIN,
  1028. .max_vblank = MT9V034_VERTICAL_BLANKING_MAX,
  1029. .min_shutter = MT9V034_TOTAL_SHUTTER_WIDTH_MIN,
  1030. .max_shutter = MT9V034_TOTAL_SHUTTER_WIDTH_MAX,
  1031. .pclk_reg = MT9V034_PIXEL_CLOCK,
  1032. .aec_max_shutter_reg = MT9V034_AEC_MAX_SHUTTER_WIDTH,
  1033. .aec_max_shutter_v4l2_ctrl = &mt9v034_aec_max_shutter_width,
  1034. },
  1035. };
  1036. static const struct mt9v032_model_info mt9v032_models[] = {
  1037. [MT9V032_MODEL_V022_COLOR] = {
  1038. .data = &mt9v032_model_data[0],
  1039. .color = true,
  1040. },
  1041. [MT9V032_MODEL_V022_MONO] = {
  1042. .data = &mt9v032_model_data[0],
  1043. .color = false,
  1044. },
  1045. [MT9V032_MODEL_V024_COLOR] = {
  1046. .data = &mt9v032_model_data[1],
  1047. .color = true,
  1048. },
  1049. [MT9V032_MODEL_V024_MONO] = {
  1050. .data = &mt9v032_model_data[1],
  1051. .color = false,
  1052. },
  1053. [MT9V032_MODEL_V032_COLOR] = {
  1054. .data = &mt9v032_model_data[0],
  1055. .color = true,
  1056. },
  1057. [MT9V032_MODEL_V032_MONO] = {
  1058. .data = &mt9v032_model_data[0],
  1059. .color = false,
  1060. },
  1061. [MT9V032_MODEL_V034_COLOR] = {
  1062. .data = &mt9v032_model_data[1],
  1063. .color = true,
  1064. },
  1065. [MT9V032_MODEL_V034_MONO] = {
  1066. .data = &mt9v032_model_data[1],
  1067. .color = false,
  1068. },
  1069. };
  1070. static const struct i2c_device_id mt9v032_id[] = {
  1071. { "mt9v022", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V022_COLOR] },
  1072. { "mt9v022m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V022_MONO] },
  1073. { "mt9v024", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V024_COLOR] },
  1074. { "mt9v024m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V024_MONO] },
  1075. { "mt9v032", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V032_COLOR] },
  1076. { "mt9v032m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V032_MONO] },
  1077. { "mt9v034", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V034_COLOR] },
  1078. { "mt9v034m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V034_MONO] },
  1079. { }
  1080. };
  1081. MODULE_DEVICE_TABLE(i2c, mt9v032_id);
  1082. #if IS_ENABLED(CONFIG_OF)
  1083. static const struct of_device_id mt9v032_of_match[] = {
  1084. { .compatible = "aptina,mt9v022" },
  1085. { .compatible = "aptina,mt9v022m" },
  1086. { .compatible = "aptina,mt9v024" },
  1087. { .compatible = "aptina,mt9v024m" },
  1088. { .compatible = "aptina,mt9v032" },
  1089. { .compatible = "aptina,mt9v032m" },
  1090. { .compatible = "aptina,mt9v034" },
  1091. { .compatible = "aptina,mt9v034m" },
  1092. { /* Sentinel */ }
  1093. };
  1094. MODULE_DEVICE_TABLE(of, mt9v032_of_match);
  1095. #endif
  1096. static struct i2c_driver mt9v032_driver = {
  1097. .driver = {
  1098. .name = "mt9v032",
  1099. .of_match_table = of_match_ptr(mt9v032_of_match),
  1100. },
  1101. .probe = mt9v032_probe,
  1102. .remove = mt9v032_remove,
  1103. .id_table = mt9v032_id,
  1104. };
  1105. module_i2c_driver(mt9v032_driver);
  1106. MODULE_DESCRIPTION("Aptina MT9V032 Camera driver");
  1107. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  1108. MODULE_LICENSE("GPL");