mt9v032.c 36 KB

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