mt9t031.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /*
  2. * Driver for MT9T031 CMOS Image Sensor from Micron
  3. *
  4. * Copyright (C) 2008, Guennadi Liakhovetski, DENX Software Engineering <lg@denx.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/videodev2.h>
  11. #include <linux/slab.h>
  12. #include <linux/i2c.h>
  13. #include <linux/log2.h>
  14. #include <media/v4l2-subdev.h>
  15. #include <media/v4l2-chip-ident.h>
  16. #include <media/soc_camera.h>
  17. /* mt9t031 i2c address 0x5d
  18. * The platform has to define i2c_board_info and link to it from
  19. * struct soc_camera_link */
  20. /* mt9t031 selected register addresses */
  21. #define MT9T031_CHIP_VERSION 0x00
  22. #define MT9T031_ROW_START 0x01
  23. #define MT9T031_COLUMN_START 0x02
  24. #define MT9T031_WINDOW_HEIGHT 0x03
  25. #define MT9T031_WINDOW_WIDTH 0x04
  26. #define MT9T031_HORIZONTAL_BLANKING 0x05
  27. #define MT9T031_VERTICAL_BLANKING 0x06
  28. #define MT9T031_OUTPUT_CONTROL 0x07
  29. #define MT9T031_SHUTTER_WIDTH_UPPER 0x08
  30. #define MT9T031_SHUTTER_WIDTH 0x09
  31. #define MT9T031_PIXEL_CLOCK_CONTROL 0x0a
  32. #define MT9T031_FRAME_RESTART 0x0b
  33. #define MT9T031_SHUTTER_DELAY 0x0c
  34. #define MT9T031_RESET 0x0d
  35. #define MT9T031_READ_MODE_1 0x1e
  36. #define MT9T031_READ_MODE_2 0x20
  37. #define MT9T031_READ_MODE_3 0x21
  38. #define MT9T031_ROW_ADDRESS_MODE 0x22
  39. #define MT9T031_COLUMN_ADDRESS_MODE 0x23
  40. #define MT9T031_GLOBAL_GAIN 0x35
  41. #define MT9T031_CHIP_ENABLE 0xF8
  42. #define MT9T031_MAX_HEIGHT 1536
  43. #define MT9T031_MAX_WIDTH 2048
  44. #define MT9T031_MIN_HEIGHT 2
  45. #define MT9T031_MIN_WIDTH 2
  46. #define MT9T031_HORIZONTAL_BLANK 142
  47. #define MT9T031_VERTICAL_BLANK 25
  48. #define MT9T031_COLUMN_SKIP 32
  49. #define MT9T031_ROW_SKIP 20
  50. #define MT9T031_BUS_PARAM (SOCAM_PCLK_SAMPLE_RISING | \
  51. SOCAM_PCLK_SAMPLE_FALLING | SOCAM_HSYNC_ACTIVE_HIGH | \
  52. SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_DATA_ACTIVE_HIGH | \
  53. SOCAM_MASTER | SOCAM_DATAWIDTH_10)
  54. static const struct soc_camera_data_format mt9t031_colour_formats[] = {
  55. {
  56. .name = "Bayer (sRGB) 10 bit",
  57. .depth = 10,
  58. .fourcc = V4L2_PIX_FMT_SGRBG10,
  59. .colorspace = V4L2_COLORSPACE_SRGB,
  60. }
  61. };
  62. struct mt9t031 {
  63. struct v4l2_subdev subdev;
  64. int model; /* V4L2_IDENT_MT9T031* codes from v4l2-chip-ident.h */
  65. unsigned char autoexposure;
  66. u16 xskip;
  67. u16 yskip;
  68. };
  69. static struct mt9t031 *to_mt9t031(const struct i2c_client *client)
  70. {
  71. return container_of(i2c_get_clientdata(client), struct mt9t031, subdev);
  72. }
  73. static int reg_read(struct i2c_client *client, const u8 reg)
  74. {
  75. s32 data = i2c_smbus_read_word_data(client, reg);
  76. return data < 0 ? data : swab16(data);
  77. }
  78. static int reg_write(struct i2c_client *client, const u8 reg,
  79. const u16 data)
  80. {
  81. return i2c_smbus_write_word_data(client, reg, swab16(data));
  82. }
  83. static int reg_set(struct i2c_client *client, const u8 reg,
  84. const u16 data)
  85. {
  86. int ret;
  87. ret = reg_read(client, reg);
  88. if (ret < 0)
  89. return ret;
  90. return reg_write(client, reg, ret | data);
  91. }
  92. static int reg_clear(struct i2c_client *client, const u8 reg,
  93. const u16 data)
  94. {
  95. int ret;
  96. ret = reg_read(client, reg);
  97. if (ret < 0)
  98. return ret;
  99. return reg_write(client, reg, ret & ~data);
  100. }
  101. static int set_shutter(struct i2c_client *client, const u32 data)
  102. {
  103. int ret;
  104. ret = reg_write(client, MT9T031_SHUTTER_WIDTH_UPPER, data >> 16);
  105. if (ret >= 0)
  106. ret = reg_write(client, MT9T031_SHUTTER_WIDTH, data & 0xffff);
  107. return ret;
  108. }
  109. static int get_shutter(struct i2c_client *client, u32 *data)
  110. {
  111. int ret;
  112. ret = reg_read(client, MT9T031_SHUTTER_WIDTH_UPPER);
  113. *data = ret << 16;
  114. if (ret >= 0)
  115. ret = reg_read(client, MT9T031_SHUTTER_WIDTH);
  116. *data |= ret & 0xffff;
  117. return ret < 0 ? ret : 0;
  118. }
  119. static int mt9t031_idle(struct i2c_client *client)
  120. {
  121. int ret;
  122. /* Disable chip output, synchronous option update */
  123. ret = reg_write(client, MT9T031_RESET, 1);
  124. if (ret >= 0)
  125. ret = reg_write(client, MT9T031_RESET, 0);
  126. if (ret >= 0)
  127. ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
  128. return ret >= 0 ? 0 : -EIO;
  129. }
  130. static int mt9t031_disable(struct i2c_client *client)
  131. {
  132. /* Disable the chip */
  133. reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
  134. return 0;
  135. }
  136. static int mt9t031_init(struct soc_camera_device *icd)
  137. {
  138. struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
  139. return mt9t031_idle(client);
  140. }
  141. static int mt9t031_release(struct soc_camera_device *icd)
  142. {
  143. struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
  144. return mt9t031_disable(client);
  145. }
  146. static int mt9t031_s_stream(struct v4l2_subdev *sd, int enable)
  147. {
  148. struct i2c_client *client = sd->priv;
  149. int ret;
  150. if (enable)
  151. /* Switch to master "normal" mode */
  152. ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 2);
  153. else
  154. /* Stop sensor readout */
  155. ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
  156. if (ret < 0)
  157. return -EIO;
  158. return 0;
  159. }
  160. static int mt9t031_set_bus_param(struct soc_camera_device *icd,
  161. unsigned long flags)
  162. {
  163. struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
  164. /* The caller should have queried our parameters, check anyway */
  165. if (flags & ~MT9T031_BUS_PARAM)
  166. return -EINVAL;
  167. if (flags & SOCAM_PCLK_SAMPLE_FALLING)
  168. reg_clear(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
  169. else
  170. reg_set(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
  171. return 0;
  172. }
  173. static unsigned long mt9t031_query_bus_param(struct soc_camera_device *icd)
  174. {
  175. struct soc_camera_link *icl = to_soc_camera_link(icd);
  176. return soc_camera_apply_sensor_flags(icl, MT9T031_BUS_PARAM);
  177. }
  178. /* Round up minima and round down maxima */
  179. static void recalculate_limits(struct soc_camera_device *icd,
  180. u16 xskip, u16 yskip)
  181. {
  182. icd->rect_max.left = (MT9T031_COLUMN_SKIP + xskip - 1) / xskip;
  183. icd->rect_max.top = (MT9T031_ROW_SKIP + yskip - 1) / yskip;
  184. icd->width_min = (MT9T031_MIN_WIDTH + xskip - 1) / xskip;
  185. icd->height_min = (MT9T031_MIN_HEIGHT + yskip - 1) / yskip;
  186. icd->rect_max.width = MT9T031_MAX_WIDTH / xskip;
  187. icd->rect_max.height = MT9T031_MAX_HEIGHT / yskip;
  188. }
  189. static int mt9t031_set_params(struct soc_camera_device *icd,
  190. struct v4l2_rect *rect, u16 xskip, u16 yskip)
  191. {
  192. struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
  193. struct mt9t031 *mt9t031 = to_mt9t031(client);
  194. int ret;
  195. u16 xbin, ybin, width, height, left, top;
  196. const u16 hblank = MT9T031_HORIZONTAL_BLANK,
  197. vblank = MT9T031_VERTICAL_BLANK;
  198. width = rect->width * xskip;
  199. height = rect->height * yskip;
  200. left = rect->left * xskip;
  201. top = rect->top * yskip;
  202. xbin = min(xskip, (u16)3);
  203. ybin = min(yskip, (u16)3);
  204. dev_dbg(&icd->dev, "xskip %u, width %u/%u, yskip %u, height %u/%u\n",
  205. xskip, width, rect->width, yskip, height, rect->height);
  206. /* Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper */
  207. switch (xbin) {
  208. case 2:
  209. left = (left + 3) & ~3;
  210. break;
  211. case 3:
  212. left = roundup(left, 6);
  213. }
  214. switch (ybin) {
  215. case 2:
  216. top = (top + 3) & ~3;
  217. break;
  218. case 3:
  219. top = roundup(top, 6);
  220. }
  221. /* Disable register update, reconfigure atomically */
  222. ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 1);
  223. if (ret < 0)
  224. return ret;
  225. /* Blanking and start values - default... */
  226. ret = reg_write(client, MT9T031_HORIZONTAL_BLANKING, hblank);
  227. if (ret >= 0)
  228. ret = reg_write(client, MT9T031_VERTICAL_BLANKING, vblank);
  229. if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) {
  230. /* Binning, skipping */
  231. if (ret >= 0)
  232. ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
  233. ((xbin - 1) << 4) | (xskip - 1));
  234. if (ret >= 0)
  235. ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
  236. ((ybin - 1) << 4) | (yskip - 1));
  237. }
  238. dev_dbg(&icd->dev, "new physical left %u, top %u\n", left, top);
  239. /* The caller provides a supported format, as guaranteed by
  240. * icd->try_fmt_cap(), soc_camera_s_crop() and soc_camera_cropcap() */
  241. if (ret >= 0)
  242. ret = reg_write(client, MT9T031_COLUMN_START, left);
  243. if (ret >= 0)
  244. ret = reg_write(client, MT9T031_ROW_START, top);
  245. if (ret >= 0)
  246. ret = reg_write(client, MT9T031_WINDOW_WIDTH, width - 1);
  247. if (ret >= 0)
  248. ret = reg_write(client, MT9T031_WINDOW_HEIGHT,
  249. height + icd->y_skip_top - 1);
  250. if (ret >= 0 && mt9t031->autoexposure) {
  251. ret = set_shutter(client, height + icd->y_skip_top + vblank);
  252. if (ret >= 0) {
  253. const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
  254. const struct v4l2_queryctrl *qctrl =
  255. soc_camera_find_qctrl(icd->ops,
  256. V4L2_CID_EXPOSURE);
  257. icd->exposure = (shutter_max / 2 + (height +
  258. icd->y_skip_top + vblank - 1) *
  259. (qctrl->maximum - qctrl->minimum)) /
  260. shutter_max + qctrl->minimum;
  261. }
  262. }
  263. /* Re-enable register update, commit all changes */
  264. if (ret >= 0)
  265. ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 1);
  266. return ret < 0 ? ret : 0;
  267. }
  268. static int mt9t031_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  269. {
  270. struct v4l2_rect *rect = &a->c;
  271. struct i2c_client *client = sd->priv;
  272. struct mt9t031 *mt9t031 = to_mt9t031(client);
  273. struct soc_camera_device *icd = client->dev.platform_data;
  274. /* Make sure we don't exceed sensor limits */
  275. if (rect->left + rect->width > icd->rect_max.left + icd->rect_max.width)
  276. rect->left = icd->rect_max.width + icd->rect_max.left -
  277. rect->width;
  278. if (rect->top + rect->height > icd->rect_max.height + icd->rect_max.top)
  279. rect->top = icd->rect_max.height + icd->rect_max.top -
  280. rect->height;
  281. /* CROP - no change in scaling, or in limits */
  282. return mt9t031_set_params(icd, rect, mt9t031->xskip, mt9t031->yskip);
  283. }
  284. static int mt9t031_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
  285. {
  286. struct i2c_client *client = sd->priv;
  287. struct mt9t031 *mt9t031 = to_mt9t031(client);
  288. struct soc_camera_device *icd = client->dev.platform_data;
  289. int ret;
  290. u16 xskip, yskip;
  291. struct v4l2_rect rect = {
  292. .left = icd->rect_current.left,
  293. .top = icd->rect_current.top,
  294. .width = f->fmt.pix.width,
  295. .height = f->fmt.pix.height,
  296. };
  297. /*
  298. * try_fmt has put rectangle within limits.
  299. * S_FMT - use binning and skipping for scaling, recalculate
  300. * limits, used for cropping
  301. */
  302. /* Is this more optimal than just a division? */
  303. for (xskip = 8; xskip > 1; xskip--)
  304. if (rect.width * xskip <= MT9T031_MAX_WIDTH)
  305. break;
  306. for (yskip = 8; yskip > 1; yskip--)
  307. if (rect.height * yskip <= MT9T031_MAX_HEIGHT)
  308. break;
  309. recalculate_limits(icd, xskip, yskip);
  310. ret = mt9t031_set_params(icd, &rect, xskip, yskip);
  311. if (!ret) {
  312. mt9t031->xskip = xskip;
  313. mt9t031->yskip = yskip;
  314. }
  315. return ret;
  316. }
  317. static int mt9t031_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
  318. {
  319. struct v4l2_pix_format *pix = &f->fmt.pix;
  320. v4l_bound_align_image(
  321. &pix->width, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH, 1,
  322. &pix->height, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT, 1, 0);
  323. return 0;
  324. }
  325. static int mt9t031_g_chip_ident(struct v4l2_subdev *sd,
  326. struct v4l2_dbg_chip_ident *id)
  327. {
  328. struct i2c_client *client = sd->priv;
  329. struct mt9t031 *mt9t031 = to_mt9t031(client);
  330. if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  331. return -EINVAL;
  332. if (id->match.addr != client->addr)
  333. return -ENODEV;
  334. id->ident = mt9t031->model;
  335. id->revision = 0;
  336. return 0;
  337. }
  338. #ifdef CONFIG_VIDEO_ADV_DEBUG
  339. static int mt9t031_g_register(struct v4l2_subdev *sd,
  340. struct v4l2_dbg_register *reg)
  341. {
  342. struct i2c_client *client = sd->priv;
  343. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  344. return -EINVAL;
  345. if (reg->match.addr != client->addr)
  346. return -ENODEV;
  347. reg->val = reg_read(client, reg->reg);
  348. if (reg->val > 0xffff)
  349. return -EIO;
  350. return 0;
  351. }
  352. static int mt9t031_s_register(struct v4l2_subdev *sd,
  353. struct v4l2_dbg_register *reg)
  354. {
  355. struct i2c_client *client = sd->priv;
  356. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  357. return -EINVAL;
  358. if (reg->match.addr != client->addr)
  359. return -ENODEV;
  360. if (reg_write(client, reg->reg, reg->val) < 0)
  361. return -EIO;
  362. return 0;
  363. }
  364. #endif
  365. static const struct v4l2_queryctrl mt9t031_controls[] = {
  366. {
  367. .id = V4L2_CID_VFLIP,
  368. .type = V4L2_CTRL_TYPE_BOOLEAN,
  369. .name = "Flip Vertically",
  370. .minimum = 0,
  371. .maximum = 1,
  372. .step = 1,
  373. .default_value = 0,
  374. }, {
  375. .id = V4L2_CID_HFLIP,
  376. .type = V4L2_CTRL_TYPE_BOOLEAN,
  377. .name = "Flip Horizontally",
  378. .minimum = 0,
  379. .maximum = 1,
  380. .step = 1,
  381. .default_value = 0,
  382. }, {
  383. .id = V4L2_CID_GAIN,
  384. .type = V4L2_CTRL_TYPE_INTEGER,
  385. .name = "Gain",
  386. .minimum = 0,
  387. .maximum = 127,
  388. .step = 1,
  389. .default_value = 64,
  390. .flags = V4L2_CTRL_FLAG_SLIDER,
  391. }, {
  392. .id = V4L2_CID_EXPOSURE,
  393. .type = V4L2_CTRL_TYPE_INTEGER,
  394. .name = "Exposure",
  395. .minimum = 1,
  396. .maximum = 255,
  397. .step = 1,
  398. .default_value = 255,
  399. .flags = V4L2_CTRL_FLAG_SLIDER,
  400. }, {
  401. .id = V4L2_CID_EXPOSURE_AUTO,
  402. .type = V4L2_CTRL_TYPE_BOOLEAN,
  403. .name = "Automatic Exposure",
  404. .minimum = 0,
  405. .maximum = 1,
  406. .step = 1,
  407. .default_value = 1,
  408. }
  409. };
  410. static struct soc_camera_ops mt9t031_ops = {
  411. .init = mt9t031_init,
  412. .release = mt9t031_release,
  413. .set_bus_param = mt9t031_set_bus_param,
  414. .query_bus_param = mt9t031_query_bus_param,
  415. .controls = mt9t031_controls,
  416. .num_controls = ARRAY_SIZE(mt9t031_controls),
  417. };
  418. static int mt9t031_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  419. {
  420. struct i2c_client *client = sd->priv;
  421. struct mt9t031 *mt9t031 = to_mt9t031(client);
  422. int data;
  423. switch (ctrl->id) {
  424. case V4L2_CID_VFLIP:
  425. data = reg_read(client, MT9T031_READ_MODE_2);
  426. if (data < 0)
  427. return -EIO;
  428. ctrl->value = !!(data & 0x8000);
  429. break;
  430. case V4L2_CID_HFLIP:
  431. data = reg_read(client, MT9T031_READ_MODE_2);
  432. if (data < 0)
  433. return -EIO;
  434. ctrl->value = !!(data & 0x4000);
  435. break;
  436. case V4L2_CID_EXPOSURE_AUTO:
  437. ctrl->value = mt9t031->autoexposure;
  438. break;
  439. }
  440. return 0;
  441. }
  442. static int mt9t031_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  443. {
  444. struct i2c_client *client = sd->priv;
  445. struct mt9t031 *mt9t031 = to_mt9t031(client);
  446. struct soc_camera_device *icd = client->dev.platform_data;
  447. const struct v4l2_queryctrl *qctrl;
  448. int data;
  449. qctrl = soc_camera_find_qctrl(&mt9t031_ops, ctrl->id);
  450. if (!qctrl)
  451. return -EINVAL;
  452. switch (ctrl->id) {
  453. case V4L2_CID_VFLIP:
  454. if (ctrl->value)
  455. data = reg_set(client, MT9T031_READ_MODE_2, 0x8000);
  456. else
  457. data = reg_clear(client, MT9T031_READ_MODE_2, 0x8000);
  458. if (data < 0)
  459. return -EIO;
  460. break;
  461. case V4L2_CID_HFLIP:
  462. if (ctrl->value)
  463. data = reg_set(client, MT9T031_READ_MODE_2, 0x4000);
  464. else
  465. data = reg_clear(client, MT9T031_READ_MODE_2, 0x4000);
  466. if (data < 0)
  467. return -EIO;
  468. break;
  469. case V4L2_CID_GAIN:
  470. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  471. return -EINVAL;
  472. /* See Datasheet Table 7, Gain settings. */
  473. if (ctrl->value <= qctrl->default_value) {
  474. /* Pack it into 0..1 step 0.125, register values 0..8 */
  475. unsigned long range = qctrl->default_value - qctrl->minimum;
  476. data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
  477. dev_dbg(&icd->dev, "Setting gain %d\n", data);
  478. data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
  479. if (data < 0)
  480. return -EIO;
  481. } else {
  482. /* Pack it into 1.125..128 variable step, register values 9..0x7860 */
  483. /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
  484. unsigned long range = qctrl->maximum - qctrl->default_value - 1;
  485. /* calculated gain: map 65..127 to 9..1024 step 0.125 */
  486. unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
  487. 1015 + range / 2) / range + 9;
  488. if (gain <= 32) /* calculated gain 9..32 -> 9..32 */
  489. data = gain;
  490. else if (gain <= 64) /* calculated gain 33..64 -> 0x51..0x60 */
  491. data = ((gain - 32) * 16 + 16) / 32 + 80;
  492. else
  493. /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */
  494. data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60;
  495. dev_dbg(&icd->dev, "Setting gain from 0x%x to 0x%x\n",
  496. reg_read(client, MT9T031_GLOBAL_GAIN), data);
  497. data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
  498. if (data < 0)
  499. return -EIO;
  500. }
  501. /* Success */
  502. icd->gain = ctrl->value;
  503. break;
  504. case V4L2_CID_EXPOSURE:
  505. /* mt9t031 has maximum == default */
  506. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  507. return -EINVAL;
  508. else {
  509. const unsigned long range = qctrl->maximum - qctrl->minimum;
  510. const u32 shutter = ((ctrl->value - qctrl->minimum) * 1048 +
  511. range / 2) / range + 1;
  512. u32 old;
  513. get_shutter(client, &old);
  514. dev_dbg(&icd->dev, "Setting shutter width from %u to %u\n",
  515. old, shutter);
  516. if (set_shutter(client, shutter) < 0)
  517. return -EIO;
  518. icd->exposure = ctrl->value;
  519. mt9t031->autoexposure = 0;
  520. }
  521. break;
  522. case V4L2_CID_EXPOSURE_AUTO:
  523. if (ctrl->value) {
  524. const u16 vblank = MT9T031_VERTICAL_BLANK;
  525. const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
  526. if (set_shutter(client, icd->rect_current.height +
  527. icd->y_skip_top + vblank) < 0)
  528. return -EIO;
  529. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
  530. icd->exposure = (shutter_max / 2 +
  531. (icd->rect_current.height +
  532. icd->y_skip_top + vblank - 1) *
  533. (qctrl->maximum - qctrl->minimum)) /
  534. shutter_max + qctrl->minimum;
  535. mt9t031->autoexposure = 1;
  536. } else
  537. mt9t031->autoexposure = 0;
  538. break;
  539. }
  540. return 0;
  541. }
  542. /* Interface active, can use i2c. If it fails, it can indeed mean, that
  543. * this wasn't our capture interface, so, we wait for the right one */
  544. static int mt9t031_video_probe(struct i2c_client *client)
  545. {
  546. struct soc_camera_device *icd = client->dev.platform_data;
  547. struct mt9t031 *mt9t031 = to_mt9t031(client);
  548. s32 data;
  549. /* We must have a parent by now. And it cannot be a wrong one.
  550. * So this entire test is completely redundant. */
  551. if (!icd->dev.parent ||
  552. to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
  553. return -ENODEV;
  554. /* Enable the chip */
  555. data = reg_write(client, MT9T031_CHIP_ENABLE, 1);
  556. dev_dbg(&icd->dev, "write: %d\n", data);
  557. /* Read out the chip version register */
  558. data = reg_read(client, MT9T031_CHIP_VERSION);
  559. switch (data) {
  560. case 0x1621:
  561. mt9t031->model = V4L2_IDENT_MT9T031;
  562. icd->formats = mt9t031_colour_formats;
  563. icd->num_formats = ARRAY_SIZE(mt9t031_colour_formats);
  564. break;
  565. default:
  566. dev_err(&icd->dev,
  567. "No MT9T031 chip detected, register read %x\n", data);
  568. return -ENODEV;
  569. }
  570. dev_info(&icd->dev, "Detected a MT9T031 chip ID %x\n", data);
  571. return 0;
  572. }
  573. static struct v4l2_subdev_core_ops mt9t031_subdev_core_ops = {
  574. .g_ctrl = mt9t031_g_ctrl,
  575. .s_ctrl = mt9t031_s_ctrl,
  576. .g_chip_ident = mt9t031_g_chip_ident,
  577. #ifdef CONFIG_VIDEO_ADV_DEBUG
  578. .g_register = mt9t031_g_register,
  579. .s_register = mt9t031_s_register,
  580. #endif
  581. };
  582. static struct v4l2_subdev_video_ops mt9t031_subdev_video_ops = {
  583. .s_stream = mt9t031_s_stream,
  584. .s_fmt = mt9t031_s_fmt,
  585. .try_fmt = mt9t031_try_fmt,
  586. .s_crop = mt9t031_s_crop,
  587. };
  588. static struct v4l2_subdev_ops mt9t031_subdev_ops = {
  589. .core = &mt9t031_subdev_core_ops,
  590. .video = &mt9t031_subdev_video_ops,
  591. };
  592. static int mt9t031_probe(struct i2c_client *client,
  593. const struct i2c_device_id *did)
  594. {
  595. struct mt9t031 *mt9t031;
  596. struct soc_camera_device *icd = client->dev.platform_data;
  597. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  598. struct soc_camera_link *icl;
  599. int ret;
  600. if (!icd) {
  601. dev_err(&client->dev, "MT9T031: missing soc-camera data!\n");
  602. return -EINVAL;
  603. }
  604. icl = to_soc_camera_link(icd);
  605. if (!icl) {
  606. dev_err(&client->dev, "MT9T031 driver needs platform data\n");
  607. return -EINVAL;
  608. }
  609. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  610. dev_warn(&adapter->dev,
  611. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  612. return -EIO;
  613. }
  614. mt9t031 = kzalloc(sizeof(struct mt9t031), GFP_KERNEL);
  615. if (!mt9t031)
  616. return -ENOMEM;
  617. v4l2_i2c_subdev_init(&mt9t031->subdev, client, &mt9t031_subdev_ops);
  618. /* Second stage probe - when a capture adapter is there */
  619. icd->ops = &mt9t031_ops;
  620. icd->rect_max.left = MT9T031_COLUMN_SKIP;
  621. icd->rect_max.top = MT9T031_ROW_SKIP;
  622. icd->rect_current.left = icd->rect_max.left;
  623. icd->rect_current.top = icd->rect_max.top;
  624. icd->width_min = MT9T031_MIN_WIDTH;
  625. icd->rect_max.width = MT9T031_MAX_WIDTH;
  626. icd->height_min = MT9T031_MIN_HEIGHT;
  627. icd->rect_max.height = MT9T031_MAX_HEIGHT;
  628. icd->y_skip_top = 0;
  629. /* Simulated autoexposure. If enabled, we calculate shutter width
  630. * ourselves in the driver based on vertical blanking and frame width */
  631. mt9t031->autoexposure = 1;
  632. mt9t031->xskip = 1;
  633. mt9t031->yskip = 1;
  634. mt9t031_idle(client);
  635. ret = mt9t031_video_probe(client);
  636. mt9t031_disable(client);
  637. if (ret) {
  638. icd->ops = NULL;
  639. i2c_set_clientdata(client, NULL);
  640. kfree(mt9t031);
  641. }
  642. return ret;
  643. }
  644. static int mt9t031_remove(struct i2c_client *client)
  645. {
  646. struct mt9t031 *mt9t031 = to_mt9t031(client);
  647. struct soc_camera_device *icd = client->dev.platform_data;
  648. icd->ops = NULL;
  649. i2c_set_clientdata(client, NULL);
  650. client->driver = NULL;
  651. kfree(mt9t031);
  652. return 0;
  653. }
  654. static const struct i2c_device_id mt9t031_id[] = {
  655. { "mt9t031", 0 },
  656. { }
  657. };
  658. MODULE_DEVICE_TABLE(i2c, mt9t031_id);
  659. static struct i2c_driver mt9t031_i2c_driver = {
  660. .driver = {
  661. .name = "mt9t031",
  662. },
  663. .probe = mt9t031_probe,
  664. .remove = mt9t031_remove,
  665. .id_table = mt9t031_id,
  666. };
  667. static int __init mt9t031_mod_init(void)
  668. {
  669. return i2c_add_driver(&mt9t031_i2c_driver);
  670. }
  671. static void __exit mt9t031_mod_exit(void)
  672. {
  673. i2c_del_driver(&mt9t031_i2c_driver);
  674. }
  675. module_init(mt9t031_mod_init);
  676. module_exit(mt9t031_mod_exit);
  677. MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
  678. MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
  679. MODULE_LICENSE("GPL v2");