mt9v032.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. * Driver for MT9V022, MT9V024, MT9V032, and MT9V034 CMOS Image Sensors
  3. *
  4. * Copyright (C) 2010, Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  5. *
  6. * Based on the MT9M001 driver,
  7. *
  8. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/delay.h>
  16. #include <linux/i2c.h>
  17. #include <linux/log2.h>
  18. #include <linux/mutex.h>
  19. #include <linux/of.h>
  20. #include <linux/of_gpio.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/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_AEC_AGC_ENABLE 0xaf
  131. #define MT9V032_AEC_ENABLE (1 << 0)
  132. #define MT9V032_AGC_ENABLE (1 << 1)
  133. #define MT9V032_THERMAL_INFO 0xc1
  134. enum mt9v032_model {
  135. MT9V032_MODEL_V022_COLOR, /* MT9V022IX7ATC */
  136. MT9V032_MODEL_V022_MONO, /* MT9V022IX7ATM */
  137. MT9V032_MODEL_V024_COLOR, /* MT9V024IA7XTC */
  138. MT9V032_MODEL_V024_MONO, /* MT9V024IA7XTM */
  139. MT9V032_MODEL_V032_COLOR, /* MT9V032C12STM */
  140. MT9V032_MODEL_V032_MONO, /* MT9V032C12STC */
  141. MT9V032_MODEL_V034_COLOR,
  142. MT9V032_MODEL_V034_MONO,
  143. };
  144. struct mt9v032_model_version {
  145. unsigned int version;
  146. const char *name;
  147. };
  148. struct mt9v032_model_data {
  149. unsigned int min_row_time;
  150. unsigned int min_hblank;
  151. unsigned int min_vblank;
  152. unsigned int max_vblank;
  153. unsigned int min_shutter;
  154. unsigned int max_shutter;
  155. unsigned int pclk_reg;
  156. };
  157. struct mt9v032_model_info {
  158. const struct mt9v032_model_data *data;
  159. bool color;
  160. };
  161. static const struct mt9v032_model_version mt9v032_versions[] = {
  162. { MT9V032_CHIP_ID_REV1, "MT9V022/MT9V032 rev1/2" },
  163. { MT9V032_CHIP_ID_REV3, "MT9V022/MT9V032 rev3" },
  164. { MT9V034_CHIP_ID_REV1, "MT9V024/MT9V034 rev1" },
  165. };
  166. static const struct mt9v032_model_data mt9v032_model_data[] = {
  167. {
  168. /* MT9V022, MT9V032 revisions 1/2/3 */
  169. .min_row_time = 660,
  170. .min_hblank = MT9V032_HORIZONTAL_BLANKING_MIN,
  171. .min_vblank = MT9V032_VERTICAL_BLANKING_MIN,
  172. .max_vblank = MT9V032_VERTICAL_BLANKING_MAX,
  173. .min_shutter = MT9V032_TOTAL_SHUTTER_WIDTH_MIN,
  174. .max_shutter = MT9V032_TOTAL_SHUTTER_WIDTH_MAX,
  175. .pclk_reg = MT9V032_PIXEL_CLOCK,
  176. }, {
  177. /* MT9V024, MT9V034 */
  178. .min_row_time = 690,
  179. .min_hblank = MT9V034_HORIZONTAL_BLANKING_MIN,
  180. .min_vblank = MT9V034_VERTICAL_BLANKING_MIN,
  181. .max_vblank = MT9V034_VERTICAL_BLANKING_MAX,
  182. .min_shutter = MT9V034_TOTAL_SHUTTER_WIDTH_MIN,
  183. .max_shutter = MT9V034_TOTAL_SHUTTER_WIDTH_MAX,
  184. .pclk_reg = MT9V034_PIXEL_CLOCK,
  185. },
  186. };
  187. static const struct mt9v032_model_info mt9v032_models[] = {
  188. [MT9V032_MODEL_V022_COLOR] = {
  189. .data = &mt9v032_model_data[0],
  190. .color = true,
  191. },
  192. [MT9V032_MODEL_V022_MONO] = {
  193. .data = &mt9v032_model_data[0],
  194. .color = false,
  195. },
  196. [MT9V032_MODEL_V024_COLOR] = {
  197. .data = &mt9v032_model_data[1],
  198. .color = true,
  199. },
  200. [MT9V032_MODEL_V024_MONO] = {
  201. .data = &mt9v032_model_data[1],
  202. .color = false,
  203. },
  204. [MT9V032_MODEL_V032_COLOR] = {
  205. .data = &mt9v032_model_data[0],
  206. .color = true,
  207. },
  208. [MT9V032_MODEL_V032_MONO] = {
  209. .data = &mt9v032_model_data[0],
  210. .color = false,
  211. },
  212. [MT9V032_MODEL_V034_COLOR] = {
  213. .data = &mt9v032_model_data[1],
  214. .color = true,
  215. },
  216. [MT9V032_MODEL_V034_MONO] = {
  217. .data = &mt9v032_model_data[1],
  218. .color = false,
  219. },
  220. };
  221. struct mt9v032 {
  222. struct v4l2_subdev subdev;
  223. struct media_pad pad;
  224. struct v4l2_mbus_framefmt format;
  225. struct v4l2_rect crop;
  226. unsigned int hratio;
  227. unsigned int vratio;
  228. struct v4l2_ctrl_handler ctrls;
  229. struct {
  230. struct v4l2_ctrl *link_freq;
  231. struct v4l2_ctrl *pixel_rate;
  232. };
  233. struct mutex power_lock;
  234. int power_count;
  235. struct regmap *regmap;
  236. struct clk *clk;
  237. struct mt9v032_platform_data *pdata;
  238. const struct mt9v032_model_info *model;
  239. const struct mt9v032_model_version *version;
  240. u32 sysclk;
  241. u16 aec_agc;
  242. u16 hblank;
  243. struct {
  244. struct v4l2_ctrl *test_pattern;
  245. struct v4l2_ctrl *test_pattern_color;
  246. };
  247. };
  248. static struct mt9v032 *to_mt9v032(struct v4l2_subdev *sd)
  249. {
  250. return container_of(sd, struct mt9v032, subdev);
  251. }
  252. static int
  253. mt9v032_update_aec_agc(struct mt9v032 *mt9v032, u16 which, int enable)
  254. {
  255. struct regmap *map = mt9v032->regmap;
  256. u16 value = mt9v032->aec_agc;
  257. int ret;
  258. if (enable)
  259. value |= which;
  260. else
  261. value &= ~which;
  262. ret = regmap_write(map, MT9V032_AEC_AGC_ENABLE, value);
  263. if (ret < 0)
  264. return ret;
  265. mt9v032->aec_agc = value;
  266. return 0;
  267. }
  268. static int
  269. mt9v032_update_hblank(struct mt9v032 *mt9v032)
  270. {
  271. struct v4l2_rect *crop = &mt9v032->crop;
  272. unsigned int min_hblank = mt9v032->model->data->min_hblank;
  273. unsigned int hblank;
  274. if (mt9v032->version->version == MT9V034_CHIP_ID_REV1)
  275. min_hblank += (mt9v032->hratio - 1) * 10;
  276. min_hblank = max_t(int, mt9v032->model->data->min_row_time - crop->width,
  277. min_hblank);
  278. hblank = max_t(unsigned int, mt9v032->hblank, min_hblank);
  279. return regmap_write(mt9v032->regmap, MT9V032_HORIZONTAL_BLANKING,
  280. hblank);
  281. }
  282. static int mt9v032_power_on(struct mt9v032 *mt9v032)
  283. {
  284. struct regmap *map = mt9v032->regmap;
  285. int ret;
  286. ret = clk_set_rate(mt9v032->clk, mt9v032->sysclk);
  287. if (ret < 0)
  288. return ret;
  289. ret = clk_prepare_enable(mt9v032->clk);
  290. if (ret)
  291. return ret;
  292. udelay(1);
  293. /* Reset the chip and stop data read out */
  294. ret = regmap_write(map, MT9V032_RESET, 1);
  295. if (ret < 0)
  296. return ret;
  297. ret = regmap_write(map, MT9V032_RESET, 0);
  298. if (ret < 0)
  299. return ret;
  300. return regmap_write(map, MT9V032_CHIP_CONTROL, 0);
  301. }
  302. static void mt9v032_power_off(struct mt9v032 *mt9v032)
  303. {
  304. clk_disable_unprepare(mt9v032->clk);
  305. }
  306. static int __mt9v032_set_power(struct mt9v032 *mt9v032, bool on)
  307. {
  308. struct regmap *map = mt9v032->regmap;
  309. int ret;
  310. if (!on) {
  311. mt9v032_power_off(mt9v032);
  312. return 0;
  313. }
  314. ret = mt9v032_power_on(mt9v032);
  315. if (ret < 0)
  316. return ret;
  317. /* Configure the pixel clock polarity */
  318. if (mt9v032->pdata && mt9v032->pdata->clk_pol) {
  319. ret = regmap_write(map, mt9v032->model->data->pclk_reg,
  320. MT9V032_PIXEL_CLOCK_INV_PXL_CLK);
  321. if (ret < 0)
  322. return ret;
  323. }
  324. /* Disable the noise correction algorithm and restore the controls. */
  325. ret = regmap_write(map, MT9V032_ROW_NOISE_CORR_CONTROL, 0);
  326. if (ret < 0)
  327. return ret;
  328. return v4l2_ctrl_handler_setup(&mt9v032->ctrls);
  329. }
  330. /* -----------------------------------------------------------------------------
  331. * V4L2 subdev video operations
  332. */
  333. static struct v4l2_mbus_framefmt *
  334. __mt9v032_get_pad_format(struct mt9v032 *mt9v032, struct v4l2_subdev_pad_config *cfg,
  335. unsigned int pad, enum v4l2_subdev_format_whence which)
  336. {
  337. switch (which) {
  338. case V4L2_SUBDEV_FORMAT_TRY:
  339. return v4l2_subdev_get_try_format(&mt9v032->subdev, cfg, pad);
  340. case V4L2_SUBDEV_FORMAT_ACTIVE:
  341. return &mt9v032->format;
  342. default:
  343. return NULL;
  344. }
  345. }
  346. static struct v4l2_rect *
  347. __mt9v032_get_pad_crop(struct mt9v032 *mt9v032, struct v4l2_subdev_pad_config *cfg,
  348. unsigned int pad, enum v4l2_subdev_format_whence which)
  349. {
  350. switch (which) {
  351. case V4L2_SUBDEV_FORMAT_TRY:
  352. return v4l2_subdev_get_try_crop(&mt9v032->subdev, cfg, pad);
  353. case V4L2_SUBDEV_FORMAT_ACTIVE:
  354. return &mt9v032->crop;
  355. default:
  356. return NULL;
  357. }
  358. }
  359. static int mt9v032_s_stream(struct v4l2_subdev *subdev, int enable)
  360. {
  361. const u16 mode = MT9V032_CHIP_CONTROL_MASTER_MODE
  362. | MT9V032_CHIP_CONTROL_DOUT_ENABLE
  363. | MT9V032_CHIP_CONTROL_SEQUENTIAL;
  364. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  365. struct v4l2_rect *crop = &mt9v032->crop;
  366. struct regmap *map = mt9v032->regmap;
  367. unsigned int hbin;
  368. unsigned int vbin;
  369. int ret;
  370. if (!enable)
  371. return regmap_update_bits(map, MT9V032_CHIP_CONTROL, mode, 0);
  372. /* Configure the window size and row/column bin */
  373. hbin = fls(mt9v032->hratio) - 1;
  374. vbin = fls(mt9v032->vratio) - 1;
  375. ret = regmap_update_bits(map, MT9V032_READ_MODE,
  376. ~MT9V032_READ_MODE_RESERVED,
  377. hbin << MT9V032_READ_MODE_COLUMN_BIN_SHIFT |
  378. vbin << MT9V032_READ_MODE_ROW_BIN_SHIFT);
  379. if (ret < 0)
  380. return ret;
  381. ret = regmap_write(map, MT9V032_COLUMN_START, crop->left);
  382. if (ret < 0)
  383. return ret;
  384. ret = regmap_write(map, MT9V032_ROW_START, crop->top);
  385. if (ret < 0)
  386. return ret;
  387. ret = regmap_write(map, MT9V032_WINDOW_WIDTH, crop->width);
  388. if (ret < 0)
  389. return ret;
  390. ret = regmap_write(map, MT9V032_WINDOW_HEIGHT, crop->height);
  391. if (ret < 0)
  392. return ret;
  393. ret = mt9v032_update_hblank(mt9v032);
  394. if (ret < 0)
  395. return ret;
  396. /* Switch to master "normal" mode */
  397. return regmap_update_bits(map, MT9V032_CHIP_CONTROL, mode, mode);
  398. }
  399. static int mt9v032_enum_mbus_code(struct v4l2_subdev *subdev,
  400. struct v4l2_subdev_pad_config *cfg,
  401. struct v4l2_subdev_mbus_code_enum *code)
  402. {
  403. if (code->index > 0)
  404. return -EINVAL;
  405. code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
  406. return 0;
  407. }
  408. static int mt9v032_enum_frame_size(struct v4l2_subdev *subdev,
  409. struct v4l2_subdev_pad_config *cfg,
  410. struct v4l2_subdev_frame_size_enum *fse)
  411. {
  412. if (fse->index >= 3 || fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
  413. return -EINVAL;
  414. fse->min_width = MT9V032_WINDOW_WIDTH_DEF / (1 << fse->index);
  415. fse->max_width = fse->min_width;
  416. fse->min_height = MT9V032_WINDOW_HEIGHT_DEF / (1 << fse->index);
  417. fse->max_height = fse->min_height;
  418. return 0;
  419. }
  420. static int mt9v032_get_format(struct v4l2_subdev *subdev,
  421. struct v4l2_subdev_pad_config *cfg,
  422. struct v4l2_subdev_format *format)
  423. {
  424. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  425. format->format = *__mt9v032_get_pad_format(mt9v032, cfg, format->pad,
  426. format->which);
  427. return 0;
  428. }
  429. static void mt9v032_configure_pixel_rate(struct mt9v032 *mt9v032)
  430. {
  431. struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
  432. int ret;
  433. ret = v4l2_ctrl_s_ctrl_int64(mt9v032->pixel_rate,
  434. mt9v032->sysclk / mt9v032->hratio);
  435. if (ret < 0)
  436. dev_warn(&client->dev, "failed to set pixel rate (%d)\n", ret);
  437. }
  438. static unsigned int mt9v032_calc_ratio(unsigned int input, unsigned int output)
  439. {
  440. /* Compute the power-of-two binning factor closest to the input size to
  441. * output size ratio. Given that the output size is bounded by input/4
  442. * and input, a generic implementation would be an ineffective luxury.
  443. */
  444. if (output * 3 > input * 2)
  445. return 1;
  446. if (output * 3 > input)
  447. return 2;
  448. return 4;
  449. }
  450. static int mt9v032_set_format(struct v4l2_subdev *subdev,
  451. struct v4l2_subdev_pad_config *cfg,
  452. struct v4l2_subdev_format *format)
  453. {
  454. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  455. struct v4l2_mbus_framefmt *__format;
  456. struct v4l2_rect *__crop;
  457. unsigned int width;
  458. unsigned int height;
  459. unsigned int hratio;
  460. unsigned int vratio;
  461. __crop = __mt9v032_get_pad_crop(mt9v032, cfg, format->pad,
  462. format->which);
  463. /* Clamp the width and height to avoid dividing by zero. */
  464. width = clamp(ALIGN(format->format.width, 2),
  465. max_t(unsigned int, __crop->width / 4,
  466. MT9V032_WINDOW_WIDTH_MIN),
  467. __crop->width);
  468. height = clamp(ALIGN(format->format.height, 2),
  469. max_t(unsigned int, __crop->height / 4,
  470. MT9V032_WINDOW_HEIGHT_MIN),
  471. __crop->height);
  472. hratio = mt9v032_calc_ratio(__crop->width, width);
  473. vratio = mt9v032_calc_ratio(__crop->height, height);
  474. __format = __mt9v032_get_pad_format(mt9v032, cfg, format->pad,
  475. format->which);
  476. __format->width = __crop->width / hratio;
  477. __format->height = __crop->height / vratio;
  478. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  479. mt9v032->hratio = hratio;
  480. mt9v032->vratio = vratio;
  481. mt9v032_configure_pixel_rate(mt9v032);
  482. }
  483. format->format = *__format;
  484. return 0;
  485. }
  486. static int mt9v032_get_selection(struct v4l2_subdev *subdev,
  487. struct v4l2_subdev_pad_config *cfg,
  488. struct v4l2_subdev_selection *sel)
  489. {
  490. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  491. if (sel->target != V4L2_SEL_TGT_CROP)
  492. return -EINVAL;
  493. sel->r = *__mt9v032_get_pad_crop(mt9v032, cfg, sel->pad, sel->which);
  494. return 0;
  495. }
  496. static int mt9v032_set_selection(struct v4l2_subdev *subdev,
  497. struct v4l2_subdev_pad_config *cfg,
  498. struct v4l2_subdev_selection *sel)
  499. {
  500. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  501. struct v4l2_mbus_framefmt *__format;
  502. struct v4l2_rect *__crop;
  503. struct v4l2_rect rect;
  504. if (sel->target != V4L2_SEL_TGT_CROP)
  505. return -EINVAL;
  506. /* Clamp the crop rectangle boundaries and align them to a non multiple
  507. * of 2 pixels to ensure a GRBG Bayer pattern.
  508. */
  509. rect.left = clamp(ALIGN(sel->r.left + 1, 2) - 1,
  510. MT9V032_COLUMN_START_MIN,
  511. MT9V032_COLUMN_START_MAX);
  512. rect.top = clamp(ALIGN(sel->r.top + 1, 2) - 1,
  513. MT9V032_ROW_START_MIN,
  514. MT9V032_ROW_START_MAX);
  515. rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
  516. MT9V032_WINDOW_WIDTH_MIN,
  517. MT9V032_WINDOW_WIDTH_MAX);
  518. rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
  519. MT9V032_WINDOW_HEIGHT_MIN,
  520. MT9V032_WINDOW_HEIGHT_MAX);
  521. rect.width = min_t(unsigned int,
  522. rect.width, MT9V032_PIXEL_ARRAY_WIDTH - rect.left);
  523. rect.height = min_t(unsigned int,
  524. rect.height, MT9V032_PIXEL_ARRAY_HEIGHT - rect.top);
  525. __crop = __mt9v032_get_pad_crop(mt9v032, cfg, sel->pad, sel->which);
  526. if (rect.width != __crop->width || rect.height != __crop->height) {
  527. /* Reset the output image size if the crop rectangle size has
  528. * been modified.
  529. */
  530. __format = __mt9v032_get_pad_format(mt9v032, cfg, sel->pad,
  531. sel->which);
  532. __format->width = rect.width;
  533. __format->height = rect.height;
  534. if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  535. mt9v032->hratio = 1;
  536. mt9v032->vratio = 1;
  537. mt9v032_configure_pixel_rate(mt9v032);
  538. }
  539. }
  540. *__crop = rect;
  541. sel->r = rect;
  542. return 0;
  543. }
  544. /* -----------------------------------------------------------------------------
  545. * V4L2 subdev control operations
  546. */
  547. #define V4L2_CID_TEST_PATTERN_COLOR (V4L2_CID_USER_BASE | 0x1001)
  548. static int mt9v032_s_ctrl(struct v4l2_ctrl *ctrl)
  549. {
  550. struct mt9v032 *mt9v032 =
  551. container_of(ctrl->handler, struct mt9v032, ctrls);
  552. struct regmap *map = mt9v032->regmap;
  553. u32 freq;
  554. u16 data;
  555. switch (ctrl->id) {
  556. case V4L2_CID_AUTOGAIN:
  557. return mt9v032_update_aec_agc(mt9v032, MT9V032_AGC_ENABLE,
  558. ctrl->val);
  559. case V4L2_CID_GAIN:
  560. return regmap_write(map, MT9V032_ANALOG_GAIN, ctrl->val);
  561. case V4L2_CID_EXPOSURE_AUTO:
  562. return mt9v032_update_aec_agc(mt9v032, MT9V032_AEC_ENABLE,
  563. !ctrl->val);
  564. case V4L2_CID_EXPOSURE:
  565. return regmap_write(map, MT9V032_TOTAL_SHUTTER_WIDTH,
  566. ctrl->val);
  567. case V4L2_CID_HBLANK:
  568. mt9v032->hblank = ctrl->val;
  569. return mt9v032_update_hblank(mt9v032);
  570. case V4L2_CID_VBLANK:
  571. return regmap_write(map, MT9V032_VERTICAL_BLANKING,
  572. ctrl->val);
  573. case V4L2_CID_PIXEL_RATE:
  574. case V4L2_CID_LINK_FREQ:
  575. if (mt9v032->link_freq == NULL)
  576. break;
  577. freq = mt9v032->pdata->link_freqs[mt9v032->link_freq->val];
  578. *mt9v032->pixel_rate->p_new.p_s64 = freq;
  579. mt9v032->sysclk = freq;
  580. break;
  581. case V4L2_CID_TEST_PATTERN:
  582. switch (mt9v032->test_pattern->val) {
  583. case 0:
  584. data = 0;
  585. break;
  586. case 1:
  587. data = MT9V032_TEST_PATTERN_GRAY_VERTICAL
  588. | MT9V032_TEST_PATTERN_ENABLE;
  589. break;
  590. case 2:
  591. data = MT9V032_TEST_PATTERN_GRAY_HORIZONTAL
  592. | MT9V032_TEST_PATTERN_ENABLE;
  593. break;
  594. case 3:
  595. data = MT9V032_TEST_PATTERN_GRAY_DIAGONAL
  596. | MT9V032_TEST_PATTERN_ENABLE;
  597. break;
  598. default:
  599. data = (mt9v032->test_pattern_color->val <<
  600. MT9V032_TEST_PATTERN_DATA_SHIFT)
  601. | MT9V032_TEST_PATTERN_USE_DATA
  602. | MT9V032_TEST_PATTERN_ENABLE
  603. | MT9V032_TEST_PATTERN_FLIP;
  604. break;
  605. }
  606. return regmap_write(map, MT9V032_TEST_PATTERN, data);
  607. }
  608. return 0;
  609. }
  610. static struct v4l2_ctrl_ops mt9v032_ctrl_ops = {
  611. .s_ctrl = mt9v032_s_ctrl,
  612. };
  613. static const char * const mt9v032_test_pattern_menu[] = {
  614. "Disabled",
  615. "Gray Vertical Shade",
  616. "Gray Horizontal Shade",
  617. "Gray Diagonal Shade",
  618. "Plain",
  619. };
  620. static const struct v4l2_ctrl_config mt9v032_test_pattern_color = {
  621. .ops = &mt9v032_ctrl_ops,
  622. .id = V4L2_CID_TEST_PATTERN_COLOR,
  623. .type = V4L2_CTRL_TYPE_INTEGER,
  624. .name = "Test Pattern Color",
  625. .min = 0,
  626. .max = 1023,
  627. .step = 1,
  628. .def = 0,
  629. .flags = 0,
  630. };
  631. /* -----------------------------------------------------------------------------
  632. * V4L2 subdev core operations
  633. */
  634. static int mt9v032_set_power(struct v4l2_subdev *subdev, int on)
  635. {
  636. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  637. int ret = 0;
  638. mutex_lock(&mt9v032->power_lock);
  639. /* If the power count is modified from 0 to != 0 or from != 0 to 0,
  640. * update the power state.
  641. */
  642. if (mt9v032->power_count == !on) {
  643. ret = __mt9v032_set_power(mt9v032, !!on);
  644. if (ret < 0)
  645. goto done;
  646. }
  647. /* Update the power count. */
  648. mt9v032->power_count += on ? 1 : -1;
  649. WARN_ON(mt9v032->power_count < 0);
  650. done:
  651. mutex_unlock(&mt9v032->power_lock);
  652. return ret;
  653. }
  654. /* -----------------------------------------------------------------------------
  655. * V4L2 subdev internal operations
  656. */
  657. static int mt9v032_registered(struct v4l2_subdev *subdev)
  658. {
  659. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  660. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  661. unsigned int i;
  662. u32 version;
  663. int ret;
  664. dev_info(&client->dev, "Probing MT9V032 at address 0x%02x\n",
  665. client->addr);
  666. ret = mt9v032_power_on(mt9v032);
  667. if (ret < 0) {
  668. dev_err(&client->dev, "MT9V032 power up failed\n");
  669. return ret;
  670. }
  671. /* Read and check the sensor version */
  672. ret = regmap_read(mt9v032->regmap, MT9V032_CHIP_VERSION, &version);
  673. if (ret < 0) {
  674. dev_err(&client->dev, "Failed reading chip version\n");
  675. return ret;
  676. }
  677. for (i = 0; i < ARRAY_SIZE(mt9v032_versions); ++i) {
  678. if (mt9v032_versions[i].version == version) {
  679. mt9v032->version = &mt9v032_versions[i];
  680. break;
  681. }
  682. }
  683. if (mt9v032->version == NULL) {
  684. dev_err(&client->dev, "Unsupported chip version 0x%04x\n",
  685. version);
  686. return -ENODEV;
  687. }
  688. mt9v032_power_off(mt9v032);
  689. dev_info(&client->dev, "%s detected at address 0x%02x\n",
  690. mt9v032->version->name, client->addr);
  691. mt9v032_configure_pixel_rate(mt9v032);
  692. return ret;
  693. }
  694. static int mt9v032_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
  695. {
  696. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  697. struct v4l2_mbus_framefmt *format;
  698. struct v4l2_rect *crop;
  699. crop = v4l2_subdev_get_try_crop(subdev, fh->pad, 0);
  700. crop->left = MT9V032_COLUMN_START_DEF;
  701. crop->top = MT9V032_ROW_START_DEF;
  702. crop->width = MT9V032_WINDOW_WIDTH_DEF;
  703. crop->height = MT9V032_WINDOW_HEIGHT_DEF;
  704. format = v4l2_subdev_get_try_format(subdev, fh->pad, 0);
  705. if (mt9v032->model->color)
  706. format->code = MEDIA_BUS_FMT_SGRBG10_1X10;
  707. else
  708. format->code = MEDIA_BUS_FMT_Y10_1X10;
  709. format->width = MT9V032_WINDOW_WIDTH_DEF;
  710. format->height = MT9V032_WINDOW_HEIGHT_DEF;
  711. format->field = V4L2_FIELD_NONE;
  712. format->colorspace = V4L2_COLORSPACE_SRGB;
  713. return mt9v032_set_power(subdev, 1);
  714. }
  715. static int mt9v032_close(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
  716. {
  717. return mt9v032_set_power(subdev, 0);
  718. }
  719. static struct v4l2_subdev_core_ops mt9v032_subdev_core_ops = {
  720. .s_power = mt9v032_set_power,
  721. };
  722. static struct v4l2_subdev_video_ops mt9v032_subdev_video_ops = {
  723. .s_stream = mt9v032_s_stream,
  724. };
  725. static struct v4l2_subdev_pad_ops mt9v032_subdev_pad_ops = {
  726. .enum_mbus_code = mt9v032_enum_mbus_code,
  727. .enum_frame_size = mt9v032_enum_frame_size,
  728. .get_fmt = mt9v032_get_format,
  729. .set_fmt = mt9v032_set_format,
  730. .get_selection = mt9v032_get_selection,
  731. .set_selection = mt9v032_set_selection,
  732. };
  733. static struct v4l2_subdev_ops mt9v032_subdev_ops = {
  734. .core = &mt9v032_subdev_core_ops,
  735. .video = &mt9v032_subdev_video_ops,
  736. .pad = &mt9v032_subdev_pad_ops,
  737. };
  738. static const struct v4l2_subdev_internal_ops mt9v032_subdev_internal_ops = {
  739. .registered = mt9v032_registered,
  740. .open = mt9v032_open,
  741. .close = mt9v032_close,
  742. };
  743. static const struct regmap_config mt9v032_regmap_config = {
  744. .reg_bits = 8,
  745. .val_bits = 16,
  746. .max_register = 0xff,
  747. .cache_type = REGCACHE_RBTREE,
  748. };
  749. /* -----------------------------------------------------------------------------
  750. * Driver initialization and probing
  751. */
  752. static struct mt9v032_platform_data *
  753. mt9v032_get_pdata(struct i2c_client *client)
  754. {
  755. struct mt9v032_platform_data *pdata = NULL;
  756. struct v4l2_of_endpoint endpoint;
  757. struct device_node *np;
  758. struct property *prop;
  759. if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
  760. return client->dev.platform_data;
  761. np = of_graph_get_next_endpoint(client->dev.of_node, NULL);
  762. if (!np)
  763. return NULL;
  764. if (v4l2_of_parse_endpoint(np, &endpoint) < 0)
  765. goto done;
  766. pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
  767. if (!pdata)
  768. goto done;
  769. prop = of_find_property(np, "link-frequencies", NULL);
  770. if (prop) {
  771. u64 *link_freqs;
  772. size_t size = prop->length / sizeof(*link_freqs);
  773. link_freqs = devm_kcalloc(&client->dev, size,
  774. sizeof(*link_freqs), GFP_KERNEL);
  775. if (!link_freqs)
  776. goto done;
  777. if (of_property_read_u64_array(np, "link-frequencies",
  778. link_freqs, size) < 0)
  779. goto done;
  780. pdata->link_freqs = link_freqs;
  781. pdata->link_def_freq = link_freqs[0];
  782. }
  783. pdata->clk_pol = !!(endpoint.bus.parallel.flags &
  784. V4L2_MBUS_PCLK_SAMPLE_RISING);
  785. done:
  786. of_node_put(np);
  787. return pdata;
  788. }
  789. static int mt9v032_probe(struct i2c_client *client,
  790. const struct i2c_device_id *did)
  791. {
  792. struct mt9v032_platform_data *pdata = mt9v032_get_pdata(client);
  793. struct mt9v032 *mt9v032;
  794. unsigned int i;
  795. int ret;
  796. if (!i2c_check_functionality(client->adapter,
  797. I2C_FUNC_SMBUS_WORD_DATA)) {
  798. dev_warn(&client->adapter->dev,
  799. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  800. return -EIO;
  801. }
  802. mt9v032 = devm_kzalloc(&client->dev, sizeof(*mt9v032), GFP_KERNEL);
  803. if (!mt9v032)
  804. return -ENOMEM;
  805. mt9v032->regmap = devm_regmap_init_i2c(client, &mt9v032_regmap_config);
  806. if (IS_ERR(mt9v032->regmap))
  807. return PTR_ERR(mt9v032->regmap);
  808. mt9v032->clk = devm_clk_get(&client->dev, NULL);
  809. if (IS_ERR(mt9v032->clk))
  810. return PTR_ERR(mt9v032->clk);
  811. mutex_init(&mt9v032->power_lock);
  812. mt9v032->pdata = pdata;
  813. mt9v032->model = (const void *)did->driver_data;
  814. v4l2_ctrl_handler_init(&mt9v032->ctrls, 10);
  815. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  816. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  817. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  818. V4L2_CID_GAIN, MT9V032_ANALOG_GAIN_MIN,
  819. MT9V032_ANALOG_GAIN_MAX, 1, MT9V032_ANALOG_GAIN_DEF);
  820. v4l2_ctrl_new_std_menu(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  821. V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL, 0,
  822. V4L2_EXPOSURE_AUTO);
  823. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  824. V4L2_CID_EXPOSURE, mt9v032->model->data->min_shutter,
  825. mt9v032->model->data->max_shutter, 1,
  826. MT9V032_TOTAL_SHUTTER_WIDTH_DEF);
  827. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  828. V4L2_CID_HBLANK, mt9v032->model->data->min_hblank,
  829. MT9V032_HORIZONTAL_BLANKING_MAX, 1,
  830. MT9V032_HORIZONTAL_BLANKING_DEF);
  831. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  832. V4L2_CID_VBLANK, mt9v032->model->data->min_vblank,
  833. mt9v032->model->data->max_vblank, 1,
  834. MT9V032_VERTICAL_BLANKING_DEF);
  835. mt9v032->test_pattern = v4l2_ctrl_new_std_menu_items(&mt9v032->ctrls,
  836. &mt9v032_ctrl_ops, V4L2_CID_TEST_PATTERN,
  837. ARRAY_SIZE(mt9v032_test_pattern_menu) - 1, 0, 0,
  838. mt9v032_test_pattern_menu);
  839. mt9v032->test_pattern_color = v4l2_ctrl_new_custom(&mt9v032->ctrls,
  840. &mt9v032_test_pattern_color, NULL);
  841. v4l2_ctrl_cluster(2, &mt9v032->test_pattern);
  842. mt9v032->pixel_rate =
  843. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  844. V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
  845. if (pdata && pdata->link_freqs) {
  846. unsigned int def = 0;
  847. for (i = 0; pdata->link_freqs[i]; ++i) {
  848. if (pdata->link_freqs[i] == pdata->link_def_freq)
  849. def = i;
  850. }
  851. mt9v032->link_freq =
  852. v4l2_ctrl_new_int_menu(&mt9v032->ctrls,
  853. &mt9v032_ctrl_ops,
  854. V4L2_CID_LINK_FREQ, i - 1, def,
  855. pdata->link_freqs);
  856. v4l2_ctrl_cluster(2, &mt9v032->link_freq);
  857. }
  858. mt9v032->subdev.ctrl_handler = &mt9v032->ctrls;
  859. if (mt9v032->ctrls.error) {
  860. dev_err(&client->dev, "control initialization error %d\n",
  861. mt9v032->ctrls.error);
  862. ret = mt9v032->ctrls.error;
  863. goto err;
  864. }
  865. mt9v032->crop.left = MT9V032_COLUMN_START_DEF;
  866. mt9v032->crop.top = MT9V032_ROW_START_DEF;
  867. mt9v032->crop.width = MT9V032_WINDOW_WIDTH_DEF;
  868. mt9v032->crop.height = MT9V032_WINDOW_HEIGHT_DEF;
  869. if (mt9v032->model->color)
  870. mt9v032->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
  871. else
  872. mt9v032->format.code = MEDIA_BUS_FMT_Y10_1X10;
  873. mt9v032->format.width = MT9V032_WINDOW_WIDTH_DEF;
  874. mt9v032->format.height = MT9V032_WINDOW_HEIGHT_DEF;
  875. mt9v032->format.field = V4L2_FIELD_NONE;
  876. mt9v032->format.colorspace = V4L2_COLORSPACE_SRGB;
  877. mt9v032->hratio = 1;
  878. mt9v032->vratio = 1;
  879. mt9v032->aec_agc = MT9V032_AEC_ENABLE | MT9V032_AGC_ENABLE;
  880. mt9v032->hblank = MT9V032_HORIZONTAL_BLANKING_DEF;
  881. mt9v032->sysclk = MT9V032_SYSCLK_FREQ_DEF;
  882. v4l2_i2c_subdev_init(&mt9v032->subdev, client, &mt9v032_subdev_ops);
  883. mt9v032->subdev.internal_ops = &mt9v032_subdev_internal_ops;
  884. mt9v032->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  885. mt9v032->pad.flags = MEDIA_PAD_FL_SOURCE;
  886. ret = media_entity_init(&mt9v032->subdev.entity, 1, &mt9v032->pad, 0);
  887. if (ret < 0)
  888. goto err;
  889. mt9v032->subdev.dev = &client->dev;
  890. ret = v4l2_async_register_subdev(&mt9v032->subdev);
  891. if (ret < 0)
  892. goto err;
  893. return 0;
  894. err:
  895. media_entity_cleanup(&mt9v032->subdev.entity);
  896. v4l2_ctrl_handler_free(&mt9v032->ctrls);
  897. return ret;
  898. }
  899. static int mt9v032_remove(struct i2c_client *client)
  900. {
  901. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  902. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  903. v4l2_async_unregister_subdev(subdev);
  904. v4l2_ctrl_handler_free(&mt9v032->ctrls);
  905. media_entity_cleanup(&subdev->entity);
  906. return 0;
  907. }
  908. static const struct i2c_device_id mt9v032_id[] = {
  909. { "mt9v022", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V022_COLOR] },
  910. { "mt9v022m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V022_MONO] },
  911. { "mt9v024", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V024_COLOR] },
  912. { "mt9v024m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V024_MONO] },
  913. { "mt9v032", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V032_COLOR] },
  914. { "mt9v032m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V032_MONO] },
  915. { "mt9v034", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V034_COLOR] },
  916. { "mt9v034m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V034_MONO] },
  917. { }
  918. };
  919. MODULE_DEVICE_TABLE(i2c, mt9v032_id);
  920. #if IS_ENABLED(CONFIG_OF)
  921. static const struct of_device_id mt9v032_of_match[] = {
  922. { .compatible = "aptina,mt9v022" },
  923. { .compatible = "aptina,mt9v022m" },
  924. { .compatible = "aptina,mt9v024" },
  925. { .compatible = "aptina,mt9v024m" },
  926. { .compatible = "aptina,mt9v032" },
  927. { .compatible = "aptina,mt9v032m" },
  928. { .compatible = "aptina,mt9v034" },
  929. { .compatible = "aptina,mt9v034m" },
  930. { /* Sentinel */ }
  931. };
  932. MODULE_DEVICE_TABLE(of, mt9v032_of_match);
  933. #endif
  934. static struct i2c_driver mt9v032_driver = {
  935. .driver = {
  936. .name = "mt9v032",
  937. .of_match_table = of_match_ptr(mt9v032_of_match),
  938. },
  939. .probe = mt9v032_probe,
  940. .remove = mt9v032_remove,
  941. .id_table = mt9v032_id,
  942. };
  943. module_i2c_driver(mt9v032_driver);
  944. MODULE_DESCRIPTION("Aptina MT9V032 Camera driver");
  945. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  946. MODULE_LICENSE("GPL");