mt9v032.c 36 KB

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