mt9m032.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for MT9M032 CMOS Image Sensor from Micron
  4. *
  5. * Copyright (C) 2010-2011 Lund Engineering
  6. * Contact: Gil Lund <[email protected]>
  7. * Author: Martin Hostettler <[email protected]>
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/i2c.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/math64.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/slab.h>
  17. #include <linux/v4l2-mediabus.h>
  18. #include <media/media-entity.h>
  19. #include <media/i2c/mt9m032.h>
  20. #include <media/v4l2-ctrls.h>
  21. #include <media/v4l2-device.h>
  22. #include <media/v4l2-subdev.h>
  23. #include "aptina-pll.h"
  24. /*
  25. * width and height include active boundary and black parts
  26. *
  27. * column 0- 15 active boundary
  28. * column 16-1455 image
  29. * column 1456-1471 active boundary
  30. * column 1472-1599 black
  31. *
  32. * row 0- 51 black
  33. * row 53- 59 active boundary
  34. * row 60-1139 image
  35. * row 1140-1147 active boundary
  36. * row 1148-1151 black
  37. */
  38. #define MT9M032_PIXEL_ARRAY_WIDTH 1600
  39. #define MT9M032_PIXEL_ARRAY_HEIGHT 1152
  40. #define MT9M032_CHIP_VERSION 0x00
  41. #define MT9M032_CHIP_VERSION_VALUE 0x1402
  42. #define MT9M032_ROW_START 0x01
  43. #define MT9M032_ROW_START_MIN 0
  44. #define MT9M032_ROW_START_MAX 1152
  45. #define MT9M032_ROW_START_DEF 60
  46. #define MT9M032_COLUMN_START 0x02
  47. #define MT9M032_COLUMN_START_MIN 0
  48. #define MT9M032_COLUMN_START_MAX 1600
  49. #define MT9M032_COLUMN_START_DEF 16
  50. #define MT9M032_ROW_SIZE 0x03
  51. #define MT9M032_ROW_SIZE_MIN 32
  52. #define MT9M032_ROW_SIZE_MAX 1152
  53. #define MT9M032_ROW_SIZE_DEF 1080
  54. #define MT9M032_COLUMN_SIZE 0x04
  55. #define MT9M032_COLUMN_SIZE_MIN 32
  56. #define MT9M032_COLUMN_SIZE_MAX 1600
  57. #define MT9M032_COLUMN_SIZE_DEF 1440
  58. #define MT9M032_HBLANK 0x05
  59. #define MT9M032_VBLANK 0x06
  60. #define MT9M032_VBLANK_MAX 0x7ff
  61. #define MT9M032_SHUTTER_WIDTH_HIGH 0x08
  62. #define MT9M032_SHUTTER_WIDTH_LOW 0x09
  63. #define MT9M032_SHUTTER_WIDTH_MIN 1
  64. #define MT9M032_SHUTTER_WIDTH_MAX 1048575
  65. #define MT9M032_SHUTTER_WIDTH_DEF 1943
  66. #define MT9M032_PIX_CLK_CTRL 0x0a
  67. #define MT9M032_PIX_CLK_CTRL_INV_PIXCLK 0x8000
  68. #define MT9M032_RESTART 0x0b
  69. #define MT9M032_RESET 0x0d
  70. #define MT9M032_PLL_CONFIG1 0x11
  71. #define MT9M032_PLL_CONFIG1_PREDIV_MASK 0x3f
  72. #define MT9M032_PLL_CONFIG1_MUL_SHIFT 8
  73. #define MT9M032_READ_MODE1 0x1e
  74. #define MT9M032_READ_MODE1_OUTPUT_BAD_FRAMES (1 << 13)
  75. #define MT9M032_READ_MODE1_MAINTAIN_FRAME_RATE (1 << 12)
  76. #define MT9M032_READ_MODE1_XOR_LINE_VALID (1 << 11)
  77. #define MT9M032_READ_MODE1_CONT_LINE_VALID (1 << 10)
  78. #define MT9M032_READ_MODE1_INVERT_TRIGGER (1 << 9)
  79. #define MT9M032_READ_MODE1_SNAPSHOT (1 << 8)
  80. #define MT9M032_READ_MODE1_GLOBAL_RESET (1 << 7)
  81. #define MT9M032_READ_MODE1_BULB_EXPOSURE (1 << 6)
  82. #define MT9M032_READ_MODE1_INVERT_STROBE (1 << 5)
  83. #define MT9M032_READ_MODE1_STROBE_ENABLE (1 << 4)
  84. #define MT9M032_READ_MODE1_STROBE_START_TRIG1 (0 << 2)
  85. #define MT9M032_READ_MODE1_STROBE_START_EXP (1 << 2)
  86. #define MT9M032_READ_MODE1_STROBE_START_SHUTTER (2 << 2)
  87. #define MT9M032_READ_MODE1_STROBE_START_TRIG2 (3 << 2)
  88. #define MT9M032_READ_MODE1_STROBE_END_TRIG1 (0 << 0)
  89. #define MT9M032_READ_MODE1_STROBE_END_EXP (1 << 0)
  90. #define MT9M032_READ_MODE1_STROBE_END_SHUTTER (2 << 0)
  91. #define MT9M032_READ_MODE1_STROBE_END_TRIG2 (3 << 0)
  92. #define MT9M032_READ_MODE2 0x20
  93. #define MT9M032_READ_MODE2_VFLIP_SHIFT 15
  94. #define MT9M032_READ_MODE2_HFLIP_SHIFT 14
  95. #define MT9M032_READ_MODE2_ROW_BLC 0x40
  96. #define MT9M032_GAIN_GREEN1 0x2b
  97. #define MT9M032_GAIN_BLUE 0x2c
  98. #define MT9M032_GAIN_RED 0x2d
  99. #define MT9M032_GAIN_GREEN2 0x2e
  100. /* write only */
  101. #define MT9M032_GAIN_ALL 0x35
  102. #define MT9M032_GAIN_DIGITAL_MASK 0x7f
  103. #define MT9M032_GAIN_DIGITAL_SHIFT 8
  104. #define MT9M032_GAIN_AMUL_SHIFT 6
  105. #define MT9M032_GAIN_ANALOG_MASK 0x3f
  106. #define MT9M032_FORMATTER1 0x9e
  107. #define MT9M032_FORMATTER1_PLL_P1_6 (1 << 8)
  108. #define MT9M032_FORMATTER1_PARALLEL (1 << 12)
  109. #define MT9M032_FORMATTER2 0x9f
  110. #define MT9M032_FORMATTER2_DOUT_EN 0x1000
  111. #define MT9M032_FORMATTER2_PIXCLK_EN 0x2000
  112. /*
  113. * The available MT9M032 datasheet is missing documentation for register 0x10
  114. * MT9P031 seems to be close enough, so use constants from that datasheet for
  115. * now.
  116. * But keep the name MT9P031 to remind us, that this isn't really confirmed
  117. * for this sensor.
  118. */
  119. #define MT9P031_PLL_CONTROL 0x10
  120. #define MT9P031_PLL_CONTROL_PWROFF 0x0050
  121. #define MT9P031_PLL_CONTROL_PWRON 0x0051
  122. #define MT9P031_PLL_CONTROL_USEPLL 0x0052
  123. struct mt9m032 {
  124. struct v4l2_subdev subdev;
  125. struct media_pad pad;
  126. struct mt9m032_platform_data *pdata;
  127. unsigned int pix_clock;
  128. struct v4l2_ctrl_handler ctrls;
  129. struct {
  130. struct v4l2_ctrl *hflip;
  131. struct v4l2_ctrl *vflip;
  132. };
  133. struct mutex lock; /* Protects streaming, format, interval and crop */
  134. bool streaming;
  135. struct v4l2_mbus_framefmt format;
  136. struct v4l2_rect crop;
  137. struct v4l2_fract frame_interval;
  138. };
  139. #define to_mt9m032(sd) container_of(sd, struct mt9m032, subdev)
  140. #define to_dev(sensor) \
  141. (&((struct i2c_client *)v4l2_get_subdevdata(&(sensor)->subdev))->dev)
  142. static int mt9m032_read(struct i2c_client *client, u8 reg)
  143. {
  144. return i2c_smbus_read_word_swapped(client, reg);
  145. }
  146. static int mt9m032_write(struct i2c_client *client, u8 reg, const u16 data)
  147. {
  148. return i2c_smbus_write_word_swapped(client, reg, data);
  149. }
  150. static u32 mt9m032_row_time(struct mt9m032 *sensor, unsigned int width)
  151. {
  152. unsigned int effective_width;
  153. u32 ns;
  154. effective_width = width + 716; /* empirical value */
  155. ns = div_u64(1000000000ULL * effective_width, sensor->pix_clock);
  156. dev_dbg(to_dev(sensor), "MT9M032 line time: %u ns\n", ns);
  157. return ns;
  158. }
  159. static int mt9m032_update_timing(struct mt9m032 *sensor,
  160. struct v4l2_fract *interval)
  161. {
  162. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  163. struct v4l2_rect *crop = &sensor->crop;
  164. unsigned int min_vblank;
  165. unsigned int vblank;
  166. u32 row_time;
  167. if (!interval)
  168. interval = &sensor->frame_interval;
  169. row_time = mt9m032_row_time(sensor, crop->width);
  170. vblank = div_u64(1000000000ULL * interval->numerator,
  171. (u64)row_time * interval->denominator)
  172. - crop->height;
  173. if (vblank > MT9M032_VBLANK_MAX) {
  174. /* hardware limits to 11 bit values */
  175. interval->denominator = 1000;
  176. interval->numerator =
  177. div_u64((crop->height + MT9M032_VBLANK_MAX) *
  178. (u64)row_time * interval->denominator,
  179. 1000000000ULL);
  180. vblank = div_u64(1000000000ULL * interval->numerator,
  181. (u64)row_time * interval->denominator)
  182. - crop->height;
  183. }
  184. /* enforce minimal 1.6ms blanking time. */
  185. min_vblank = 1600000 / row_time;
  186. vblank = clamp_t(unsigned int, vblank, min_vblank, MT9M032_VBLANK_MAX);
  187. return mt9m032_write(client, MT9M032_VBLANK, vblank);
  188. }
  189. static int mt9m032_update_geom_timing(struct mt9m032 *sensor)
  190. {
  191. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  192. int ret;
  193. ret = mt9m032_write(client, MT9M032_COLUMN_SIZE,
  194. sensor->crop.width - 1);
  195. if (!ret)
  196. ret = mt9m032_write(client, MT9M032_ROW_SIZE,
  197. sensor->crop.height - 1);
  198. if (!ret)
  199. ret = mt9m032_write(client, MT9M032_COLUMN_START,
  200. sensor->crop.left);
  201. if (!ret)
  202. ret = mt9m032_write(client, MT9M032_ROW_START,
  203. sensor->crop.top);
  204. if (!ret)
  205. ret = mt9m032_update_timing(sensor, NULL);
  206. return ret;
  207. }
  208. static int update_formatter2(struct mt9m032 *sensor, bool streaming)
  209. {
  210. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  211. u16 reg_val = MT9M032_FORMATTER2_DOUT_EN
  212. | 0x0070; /* parts reserved! */
  213. /* possibly for changing to 14-bit mode */
  214. if (streaming)
  215. reg_val |= MT9M032_FORMATTER2_PIXCLK_EN; /* pixclock enable */
  216. return mt9m032_write(client, MT9M032_FORMATTER2, reg_val);
  217. }
  218. static int mt9m032_setup_pll(struct mt9m032 *sensor)
  219. {
  220. static const struct aptina_pll_limits limits = {
  221. .ext_clock_min = 8000000,
  222. .ext_clock_max = 16500000,
  223. .int_clock_min = 2000000,
  224. .int_clock_max = 24000000,
  225. .out_clock_min = 322000000,
  226. .out_clock_max = 693000000,
  227. .pix_clock_max = 99000000,
  228. .n_min = 1,
  229. .n_max = 64,
  230. .m_min = 16,
  231. .m_max = 255,
  232. .p1_min = 6,
  233. .p1_max = 7,
  234. };
  235. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  236. struct mt9m032_platform_data *pdata = sensor->pdata;
  237. struct aptina_pll pll;
  238. u16 reg_val;
  239. int ret;
  240. pll.ext_clock = pdata->ext_clock;
  241. pll.pix_clock = pdata->pix_clock;
  242. ret = aptina_pll_calculate(&client->dev, &limits, &pll);
  243. if (ret < 0)
  244. return ret;
  245. sensor->pix_clock = pdata->pix_clock;
  246. ret = mt9m032_write(client, MT9M032_PLL_CONFIG1,
  247. (pll.m << MT9M032_PLL_CONFIG1_MUL_SHIFT) |
  248. ((pll.n - 1) & MT9M032_PLL_CONFIG1_PREDIV_MASK));
  249. if (!ret)
  250. ret = mt9m032_write(client, MT9P031_PLL_CONTROL,
  251. MT9P031_PLL_CONTROL_PWRON |
  252. MT9P031_PLL_CONTROL_USEPLL);
  253. if (!ret) /* more reserved, Continuous, Master Mode */
  254. ret = mt9m032_write(client, MT9M032_READ_MODE1, 0x8000 |
  255. MT9M032_READ_MODE1_STROBE_START_EXP |
  256. MT9M032_READ_MODE1_STROBE_END_SHUTTER);
  257. if (!ret) {
  258. reg_val = (pll.p1 == 6 ? MT9M032_FORMATTER1_PLL_P1_6 : 0)
  259. | MT9M032_FORMATTER1_PARALLEL | 0x001e; /* 14-bit */
  260. ret = mt9m032_write(client, MT9M032_FORMATTER1, reg_val);
  261. }
  262. return ret;
  263. }
  264. /* -----------------------------------------------------------------------------
  265. * Subdev pad operations
  266. */
  267. static int mt9m032_enum_mbus_code(struct v4l2_subdev *subdev,
  268. struct v4l2_subdev_state *sd_state,
  269. struct v4l2_subdev_mbus_code_enum *code)
  270. {
  271. if (code->index != 0)
  272. return -EINVAL;
  273. code->code = MEDIA_BUS_FMT_Y8_1X8;
  274. return 0;
  275. }
  276. static int mt9m032_enum_frame_size(struct v4l2_subdev *subdev,
  277. struct v4l2_subdev_state *sd_state,
  278. struct v4l2_subdev_frame_size_enum *fse)
  279. {
  280. if (fse->index != 0 || fse->code != MEDIA_BUS_FMT_Y8_1X8)
  281. return -EINVAL;
  282. fse->min_width = MT9M032_COLUMN_SIZE_DEF;
  283. fse->max_width = MT9M032_COLUMN_SIZE_DEF;
  284. fse->min_height = MT9M032_ROW_SIZE_DEF;
  285. fse->max_height = MT9M032_ROW_SIZE_DEF;
  286. return 0;
  287. }
  288. /**
  289. * __mt9m032_get_pad_crop() - get crop rect
  290. * @sensor: pointer to the sensor struct
  291. * @sd_state: v4l2_subdev_state for getting the try crop rect from
  292. * @which: select try or active crop rect
  293. *
  294. * Returns a pointer the current active or fh relative try crop rect
  295. */
  296. static struct v4l2_rect *
  297. __mt9m032_get_pad_crop(struct mt9m032 *sensor,
  298. struct v4l2_subdev_state *sd_state,
  299. enum v4l2_subdev_format_whence which)
  300. {
  301. switch (which) {
  302. case V4L2_SUBDEV_FORMAT_TRY:
  303. return v4l2_subdev_get_try_crop(&sensor->subdev, sd_state, 0);
  304. case V4L2_SUBDEV_FORMAT_ACTIVE:
  305. return &sensor->crop;
  306. default:
  307. return NULL;
  308. }
  309. }
  310. /**
  311. * __mt9m032_get_pad_format() - get format
  312. * @sensor: pointer to the sensor struct
  313. * @sd_state: v4l2_subdev_state for getting the try format from
  314. * @which: select try or active format
  315. *
  316. * Returns a pointer the current active or fh relative try format
  317. */
  318. static struct v4l2_mbus_framefmt *
  319. __mt9m032_get_pad_format(struct mt9m032 *sensor,
  320. struct v4l2_subdev_state *sd_state,
  321. enum v4l2_subdev_format_whence which)
  322. {
  323. switch (which) {
  324. case V4L2_SUBDEV_FORMAT_TRY:
  325. return v4l2_subdev_get_try_format(&sensor->subdev, sd_state,
  326. 0);
  327. case V4L2_SUBDEV_FORMAT_ACTIVE:
  328. return &sensor->format;
  329. default:
  330. return NULL;
  331. }
  332. }
  333. static int mt9m032_get_pad_format(struct v4l2_subdev *subdev,
  334. struct v4l2_subdev_state *sd_state,
  335. struct v4l2_subdev_format *fmt)
  336. {
  337. struct mt9m032 *sensor = to_mt9m032(subdev);
  338. mutex_lock(&sensor->lock);
  339. fmt->format = *__mt9m032_get_pad_format(sensor, sd_state, fmt->which);
  340. mutex_unlock(&sensor->lock);
  341. return 0;
  342. }
  343. static int mt9m032_set_pad_format(struct v4l2_subdev *subdev,
  344. struct v4l2_subdev_state *sd_state,
  345. struct v4l2_subdev_format *fmt)
  346. {
  347. struct mt9m032 *sensor = to_mt9m032(subdev);
  348. int ret;
  349. mutex_lock(&sensor->lock);
  350. if (sensor->streaming && fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  351. ret = -EBUSY;
  352. goto done;
  353. }
  354. /* Scaling is not supported, the format is thus fixed. */
  355. fmt->format = *__mt9m032_get_pad_format(sensor, sd_state, fmt->which);
  356. ret = 0;
  357. done:
  358. mutex_unlock(&sensor->lock);
  359. return ret;
  360. }
  361. static int mt9m032_get_pad_selection(struct v4l2_subdev *subdev,
  362. struct v4l2_subdev_state *sd_state,
  363. struct v4l2_subdev_selection *sel)
  364. {
  365. struct mt9m032 *sensor = to_mt9m032(subdev);
  366. if (sel->target != V4L2_SEL_TGT_CROP)
  367. return -EINVAL;
  368. mutex_lock(&sensor->lock);
  369. sel->r = *__mt9m032_get_pad_crop(sensor, sd_state, sel->which);
  370. mutex_unlock(&sensor->lock);
  371. return 0;
  372. }
  373. static int mt9m032_set_pad_selection(struct v4l2_subdev *subdev,
  374. struct v4l2_subdev_state *sd_state,
  375. struct v4l2_subdev_selection *sel)
  376. {
  377. struct mt9m032 *sensor = to_mt9m032(subdev);
  378. struct v4l2_mbus_framefmt *format;
  379. struct v4l2_rect *__crop;
  380. struct v4l2_rect rect;
  381. int ret = 0;
  382. if (sel->target != V4L2_SEL_TGT_CROP)
  383. return -EINVAL;
  384. mutex_lock(&sensor->lock);
  385. if (sensor->streaming && sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  386. ret = -EBUSY;
  387. goto done;
  388. }
  389. /* Clamp the crop rectangle boundaries and align them to a multiple of 2
  390. * pixels to ensure a GRBG Bayer pattern.
  391. */
  392. rect.left = clamp(ALIGN(sel->r.left, 2), MT9M032_COLUMN_START_MIN,
  393. MT9M032_COLUMN_START_MAX);
  394. rect.top = clamp(ALIGN(sel->r.top, 2), MT9M032_ROW_START_MIN,
  395. MT9M032_ROW_START_MAX);
  396. rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
  397. MT9M032_COLUMN_SIZE_MIN, MT9M032_COLUMN_SIZE_MAX);
  398. rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
  399. MT9M032_ROW_SIZE_MIN, MT9M032_ROW_SIZE_MAX);
  400. rect.width = min_t(unsigned int, rect.width,
  401. MT9M032_PIXEL_ARRAY_WIDTH - rect.left);
  402. rect.height = min_t(unsigned int, rect.height,
  403. MT9M032_PIXEL_ARRAY_HEIGHT - rect.top);
  404. __crop = __mt9m032_get_pad_crop(sensor, sd_state, sel->which);
  405. if (rect.width != __crop->width || rect.height != __crop->height) {
  406. /* Reset the output image size if the crop rectangle size has
  407. * been modified.
  408. */
  409. format = __mt9m032_get_pad_format(sensor, sd_state,
  410. sel->which);
  411. format->width = rect.width;
  412. format->height = rect.height;
  413. }
  414. *__crop = rect;
  415. sel->r = rect;
  416. if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  417. ret = mt9m032_update_geom_timing(sensor);
  418. done:
  419. mutex_unlock(&sensor->lock);
  420. return ret;
  421. }
  422. static int mt9m032_get_frame_interval(struct v4l2_subdev *subdev,
  423. struct v4l2_subdev_frame_interval *fi)
  424. {
  425. struct mt9m032 *sensor = to_mt9m032(subdev);
  426. mutex_lock(&sensor->lock);
  427. memset(fi, 0, sizeof(*fi));
  428. fi->interval = sensor->frame_interval;
  429. mutex_unlock(&sensor->lock);
  430. return 0;
  431. }
  432. static int mt9m032_set_frame_interval(struct v4l2_subdev *subdev,
  433. struct v4l2_subdev_frame_interval *fi)
  434. {
  435. struct mt9m032 *sensor = to_mt9m032(subdev);
  436. int ret;
  437. mutex_lock(&sensor->lock);
  438. if (sensor->streaming) {
  439. ret = -EBUSY;
  440. goto done;
  441. }
  442. /* Avoid divisions by 0. */
  443. if (fi->interval.denominator == 0)
  444. fi->interval.denominator = 1;
  445. ret = mt9m032_update_timing(sensor, &fi->interval);
  446. if (!ret)
  447. sensor->frame_interval = fi->interval;
  448. done:
  449. mutex_unlock(&sensor->lock);
  450. return ret;
  451. }
  452. static int mt9m032_s_stream(struct v4l2_subdev *subdev, int streaming)
  453. {
  454. struct mt9m032 *sensor = to_mt9m032(subdev);
  455. int ret;
  456. mutex_lock(&sensor->lock);
  457. ret = update_formatter2(sensor, streaming);
  458. if (!ret)
  459. sensor->streaming = streaming;
  460. mutex_unlock(&sensor->lock);
  461. return ret;
  462. }
  463. /* -----------------------------------------------------------------------------
  464. * V4L2 subdev core operations
  465. */
  466. #ifdef CONFIG_VIDEO_ADV_DEBUG
  467. static int mt9m032_g_register(struct v4l2_subdev *sd,
  468. struct v4l2_dbg_register *reg)
  469. {
  470. struct mt9m032 *sensor = to_mt9m032(sd);
  471. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  472. int val;
  473. if (reg->reg > 0xff)
  474. return -EINVAL;
  475. val = mt9m032_read(client, reg->reg);
  476. if (val < 0)
  477. return -EIO;
  478. reg->size = 2;
  479. reg->val = val;
  480. return 0;
  481. }
  482. static int mt9m032_s_register(struct v4l2_subdev *sd,
  483. const struct v4l2_dbg_register *reg)
  484. {
  485. struct mt9m032 *sensor = to_mt9m032(sd);
  486. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  487. if (reg->reg > 0xff)
  488. return -EINVAL;
  489. return mt9m032_write(client, reg->reg, reg->val);
  490. }
  491. #endif
  492. /* -----------------------------------------------------------------------------
  493. * V4L2 subdev control operations
  494. */
  495. static int update_read_mode2(struct mt9m032 *sensor, bool vflip, bool hflip)
  496. {
  497. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  498. int reg_val = (vflip << MT9M032_READ_MODE2_VFLIP_SHIFT)
  499. | (hflip << MT9M032_READ_MODE2_HFLIP_SHIFT)
  500. | MT9M032_READ_MODE2_ROW_BLC
  501. | 0x0007;
  502. return mt9m032_write(client, MT9M032_READ_MODE2, reg_val);
  503. }
  504. static int mt9m032_set_gain(struct mt9m032 *sensor, s32 val)
  505. {
  506. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  507. int digital_gain_val; /* in 1/8th (0..127) */
  508. int analog_mul; /* 0 or 1 */
  509. int analog_gain_val; /* in 1/16th. (0..63) */
  510. u16 reg_val;
  511. digital_gain_val = 51; /* from setup example */
  512. if (val < 63) {
  513. analog_mul = 0;
  514. analog_gain_val = val;
  515. } else {
  516. analog_mul = 1;
  517. analog_gain_val = val / 2;
  518. }
  519. /* a_gain = (1 + analog_mul) + (analog_gain_val + 1) / 16 */
  520. /* overall_gain = a_gain * (1 + digital_gain_val / 8) */
  521. reg_val = ((digital_gain_val & MT9M032_GAIN_DIGITAL_MASK)
  522. << MT9M032_GAIN_DIGITAL_SHIFT)
  523. | ((analog_mul & 1) << MT9M032_GAIN_AMUL_SHIFT)
  524. | (analog_gain_val & MT9M032_GAIN_ANALOG_MASK);
  525. return mt9m032_write(client, MT9M032_GAIN_ALL, reg_val);
  526. }
  527. static int mt9m032_try_ctrl(struct v4l2_ctrl *ctrl)
  528. {
  529. if (ctrl->id == V4L2_CID_GAIN && ctrl->val >= 63) {
  530. /* round because of multiplier used for values >= 63 */
  531. ctrl->val &= ~1;
  532. }
  533. return 0;
  534. }
  535. static int mt9m032_set_ctrl(struct v4l2_ctrl *ctrl)
  536. {
  537. struct mt9m032 *sensor =
  538. container_of(ctrl->handler, struct mt9m032, ctrls);
  539. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  540. int ret;
  541. switch (ctrl->id) {
  542. case V4L2_CID_GAIN:
  543. return mt9m032_set_gain(sensor, ctrl->val);
  544. case V4L2_CID_HFLIP:
  545. /* case V4L2_CID_VFLIP: -- In the same cluster */
  546. return update_read_mode2(sensor, sensor->vflip->val,
  547. sensor->hflip->val);
  548. case V4L2_CID_EXPOSURE:
  549. ret = mt9m032_write(client, MT9M032_SHUTTER_WIDTH_HIGH,
  550. (ctrl->val >> 16) & 0xffff);
  551. if (ret < 0)
  552. return ret;
  553. return mt9m032_write(client, MT9M032_SHUTTER_WIDTH_LOW,
  554. ctrl->val & 0xffff);
  555. }
  556. return 0;
  557. }
  558. static const struct v4l2_ctrl_ops mt9m032_ctrl_ops = {
  559. .s_ctrl = mt9m032_set_ctrl,
  560. .try_ctrl = mt9m032_try_ctrl,
  561. };
  562. /* -------------------------------------------------------------------------- */
  563. static const struct v4l2_subdev_core_ops mt9m032_core_ops = {
  564. #ifdef CONFIG_VIDEO_ADV_DEBUG
  565. .g_register = mt9m032_g_register,
  566. .s_register = mt9m032_s_register,
  567. #endif
  568. };
  569. static const struct v4l2_subdev_video_ops mt9m032_video_ops = {
  570. .s_stream = mt9m032_s_stream,
  571. .g_frame_interval = mt9m032_get_frame_interval,
  572. .s_frame_interval = mt9m032_set_frame_interval,
  573. };
  574. static const struct v4l2_subdev_pad_ops mt9m032_pad_ops = {
  575. .enum_mbus_code = mt9m032_enum_mbus_code,
  576. .enum_frame_size = mt9m032_enum_frame_size,
  577. .get_fmt = mt9m032_get_pad_format,
  578. .set_fmt = mt9m032_set_pad_format,
  579. .set_selection = mt9m032_set_pad_selection,
  580. .get_selection = mt9m032_get_pad_selection,
  581. };
  582. static const struct v4l2_subdev_ops mt9m032_ops = {
  583. .core = &mt9m032_core_ops,
  584. .video = &mt9m032_video_ops,
  585. .pad = &mt9m032_pad_ops,
  586. };
  587. /* -----------------------------------------------------------------------------
  588. * Driver initialization and probing
  589. */
  590. static int mt9m032_probe(struct i2c_client *client,
  591. const struct i2c_device_id *devid)
  592. {
  593. struct mt9m032_platform_data *pdata = client->dev.platform_data;
  594. struct i2c_adapter *adapter = client->adapter;
  595. struct mt9m032 *sensor;
  596. int chip_version;
  597. int ret;
  598. if (pdata == NULL) {
  599. dev_err(&client->dev, "No platform data\n");
  600. return -EINVAL;
  601. }
  602. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  603. dev_warn(&client->dev,
  604. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  605. return -EIO;
  606. }
  607. if (!client->dev.platform_data)
  608. return -ENODEV;
  609. sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
  610. if (sensor == NULL)
  611. return -ENOMEM;
  612. mutex_init(&sensor->lock);
  613. sensor->pdata = pdata;
  614. v4l2_i2c_subdev_init(&sensor->subdev, client, &mt9m032_ops);
  615. sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  616. chip_version = mt9m032_read(client, MT9M032_CHIP_VERSION);
  617. if (chip_version != MT9M032_CHIP_VERSION_VALUE) {
  618. dev_err(&client->dev, "MT9M032 not detected, wrong version "
  619. "0x%04x\n", chip_version);
  620. ret = -ENODEV;
  621. goto error_sensor;
  622. }
  623. dev_info(&client->dev, "MT9M032 detected at address 0x%02x\n",
  624. client->addr);
  625. sensor->frame_interval.numerator = 1;
  626. sensor->frame_interval.denominator = 30;
  627. sensor->crop.left = MT9M032_COLUMN_START_DEF;
  628. sensor->crop.top = MT9M032_ROW_START_DEF;
  629. sensor->crop.width = MT9M032_COLUMN_SIZE_DEF;
  630. sensor->crop.height = MT9M032_ROW_SIZE_DEF;
  631. sensor->format.width = sensor->crop.width;
  632. sensor->format.height = sensor->crop.height;
  633. sensor->format.code = MEDIA_BUS_FMT_Y8_1X8;
  634. sensor->format.field = V4L2_FIELD_NONE;
  635. sensor->format.colorspace = V4L2_COLORSPACE_SRGB;
  636. v4l2_ctrl_handler_init(&sensor->ctrls, 5);
  637. v4l2_ctrl_new_std(&sensor->ctrls, &mt9m032_ctrl_ops,
  638. V4L2_CID_GAIN, 0, 127, 1, 64);
  639. sensor->hflip = v4l2_ctrl_new_std(&sensor->ctrls,
  640. &mt9m032_ctrl_ops,
  641. V4L2_CID_HFLIP, 0, 1, 1, 0);
  642. sensor->vflip = v4l2_ctrl_new_std(&sensor->ctrls,
  643. &mt9m032_ctrl_ops,
  644. V4L2_CID_VFLIP, 0, 1, 1, 0);
  645. v4l2_ctrl_new_std(&sensor->ctrls, &mt9m032_ctrl_ops,
  646. V4L2_CID_EXPOSURE, MT9M032_SHUTTER_WIDTH_MIN,
  647. MT9M032_SHUTTER_WIDTH_MAX, 1,
  648. MT9M032_SHUTTER_WIDTH_DEF);
  649. v4l2_ctrl_new_std(&sensor->ctrls, &mt9m032_ctrl_ops,
  650. V4L2_CID_PIXEL_RATE, pdata->pix_clock,
  651. pdata->pix_clock, 1, pdata->pix_clock);
  652. if (sensor->ctrls.error) {
  653. ret = sensor->ctrls.error;
  654. dev_err(&client->dev, "control initialization error %d\n", ret);
  655. goto error_ctrl;
  656. }
  657. v4l2_ctrl_cluster(2, &sensor->hflip);
  658. sensor->subdev.ctrl_handler = &sensor->ctrls;
  659. sensor->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  660. sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
  661. ret = media_entity_pads_init(&sensor->subdev.entity, 1, &sensor->pad);
  662. if (ret < 0)
  663. goto error_ctrl;
  664. ret = mt9m032_write(client, MT9M032_RESET, 1); /* reset on */
  665. if (ret < 0)
  666. goto error_entity;
  667. ret = mt9m032_write(client, MT9M032_RESET, 0); /* reset off */
  668. if (ret < 0)
  669. goto error_entity;
  670. ret = mt9m032_setup_pll(sensor);
  671. if (ret < 0)
  672. goto error_entity;
  673. usleep_range(10000, 11000);
  674. ret = v4l2_ctrl_handler_setup(&sensor->ctrls);
  675. if (ret < 0)
  676. goto error_entity;
  677. /* SIZE */
  678. ret = mt9m032_update_geom_timing(sensor);
  679. if (ret < 0)
  680. goto error_entity;
  681. ret = mt9m032_write(client, 0x41, 0x0000); /* reserved !!! */
  682. if (ret < 0)
  683. goto error_entity;
  684. ret = mt9m032_write(client, 0x42, 0x0003); /* reserved !!! */
  685. if (ret < 0)
  686. goto error_entity;
  687. ret = mt9m032_write(client, 0x43, 0x0003); /* reserved !!! */
  688. if (ret < 0)
  689. goto error_entity;
  690. ret = mt9m032_write(client, 0x7f, 0x0000); /* reserved !!! */
  691. if (ret < 0)
  692. goto error_entity;
  693. if (sensor->pdata->invert_pixclock) {
  694. ret = mt9m032_write(client, MT9M032_PIX_CLK_CTRL,
  695. MT9M032_PIX_CLK_CTRL_INV_PIXCLK);
  696. if (ret < 0)
  697. goto error_entity;
  698. }
  699. ret = mt9m032_write(client, MT9M032_RESTART, 1); /* Restart on */
  700. if (ret < 0)
  701. goto error_entity;
  702. msleep(100);
  703. ret = mt9m032_write(client, MT9M032_RESTART, 0); /* Restart off */
  704. if (ret < 0)
  705. goto error_entity;
  706. msleep(100);
  707. ret = update_formatter2(sensor, false);
  708. if (ret < 0)
  709. goto error_entity;
  710. return ret;
  711. error_entity:
  712. media_entity_cleanup(&sensor->subdev.entity);
  713. error_ctrl:
  714. v4l2_ctrl_handler_free(&sensor->ctrls);
  715. error_sensor:
  716. mutex_destroy(&sensor->lock);
  717. return ret;
  718. }
  719. static void mt9m032_remove(struct i2c_client *client)
  720. {
  721. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  722. struct mt9m032 *sensor = to_mt9m032(subdev);
  723. v4l2_device_unregister_subdev(subdev);
  724. v4l2_ctrl_handler_free(&sensor->ctrls);
  725. media_entity_cleanup(&subdev->entity);
  726. mutex_destroy(&sensor->lock);
  727. }
  728. static const struct i2c_device_id mt9m032_id_table[] = {
  729. { MT9M032_NAME, 0 },
  730. { }
  731. };
  732. MODULE_DEVICE_TABLE(i2c, mt9m032_id_table);
  733. static struct i2c_driver mt9m032_i2c_driver = {
  734. .driver = {
  735. .name = MT9M032_NAME,
  736. },
  737. .probe = mt9m032_probe,
  738. .remove = mt9m032_remove,
  739. .id_table = mt9m032_id_table,
  740. };
  741. module_i2c_driver(mt9m032_i2c_driver);
  742. MODULE_AUTHOR("Martin Hostettler <[email protected]>");
  743. MODULE_DESCRIPTION("MT9M032 camera sensor driver");
  744. MODULE_LICENSE("GPL v2");