mt9v011.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * mt9v011 -Micron 1/4-Inch VGA Digital Image Sensor
  3. *
  4. * Copyright (c) 2009 Mauro Carvalho Chehab (mchehab@redhat.com)
  5. * This code is placed under the terms of the GNU General Public License v2
  6. */
  7. #include <linux/i2c.h>
  8. #include <linux/videodev2.h>
  9. #include <linux/delay.h>
  10. #include <asm/div64.h>
  11. #include <media/v4l2-device.h>
  12. #include "mt9v011.h"
  13. #include <media/v4l2-i2c-drv.h>
  14. #include <media/v4l2-chip-ident.h>
  15. MODULE_DESCRIPTION("Micron mt9v011 sensor driver");
  16. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  17. MODULE_LICENSE("GPL");
  18. static int debug;
  19. module_param(debug, int, 0);
  20. MODULE_PARM_DESC(debug, "Debug level (0-2)");
  21. /* supported controls */
  22. static struct v4l2_queryctrl mt9v011_qctrl[] = {
  23. {
  24. .id = V4L2_CID_GAIN,
  25. .type = V4L2_CTRL_TYPE_INTEGER,
  26. .name = "Gain",
  27. .minimum = 0,
  28. .maximum = (1 << 10) - 1,
  29. .step = 1,
  30. .default_value = 0x0020,
  31. .flags = 0,
  32. }, {
  33. .id = V4L2_CID_RED_BALANCE,
  34. .type = V4L2_CTRL_TYPE_INTEGER,
  35. .name = "Red Balance",
  36. .minimum = -1 << 9,
  37. .maximum = (1 << 9) - 1,
  38. .step = 1,
  39. .default_value = 0,
  40. .flags = 0,
  41. }, {
  42. .id = V4L2_CID_BLUE_BALANCE,
  43. .type = V4L2_CTRL_TYPE_INTEGER,
  44. .name = "Blue Balance",
  45. .minimum = -1 << 9,
  46. .maximum = (1 << 9) - 1,
  47. .step = 1,
  48. .default_value = 0,
  49. .flags = 0,
  50. },
  51. };
  52. struct mt9v011 {
  53. struct v4l2_subdev sd;
  54. unsigned width, height;
  55. unsigned xtal;
  56. u16 global_gain, red_bal, blue_bal;
  57. };
  58. static inline struct mt9v011 *to_mt9v011(struct v4l2_subdev *sd)
  59. {
  60. return container_of(sd, struct mt9v011, sd);
  61. }
  62. static int mt9v011_read(struct v4l2_subdev *sd, unsigned char addr)
  63. {
  64. struct i2c_client *c = v4l2_get_subdevdata(sd);
  65. __be16 buffer;
  66. int rc, val;
  67. rc = i2c_master_send(c, &addr, 1);
  68. if (rc != 1)
  69. v4l2_dbg(0, debug, sd,
  70. "i2c i/o error: rc == %d (should be 1)\n", rc);
  71. msleep(10);
  72. rc = i2c_master_recv(c, (char *)&buffer, 2);
  73. if (rc != 2)
  74. v4l2_dbg(0, debug, sd,
  75. "i2c i/o error: rc == %d (should be 2)\n", rc);
  76. val = be16_to_cpu(buffer);
  77. v4l2_dbg(2, debug, sd, "mt9v011: read 0x%02x = 0x%04x\n", addr, val);
  78. return val;
  79. }
  80. static void mt9v011_write(struct v4l2_subdev *sd, unsigned char addr,
  81. u16 value)
  82. {
  83. struct i2c_client *c = v4l2_get_subdevdata(sd);
  84. unsigned char buffer[3];
  85. int rc;
  86. buffer[0] = addr;
  87. buffer[1] = value >> 8;
  88. buffer[2] = value & 0xff;
  89. v4l2_dbg(2, debug, sd,
  90. "mt9v011: writing 0x%02x 0x%04x\n", buffer[0], value);
  91. rc = i2c_master_send(c, buffer, 3);
  92. if (rc != 3)
  93. v4l2_dbg(0, debug, sd,
  94. "i2c i/o error: rc == %d (should be 3)\n", rc);
  95. }
  96. struct i2c_reg_value {
  97. unsigned char reg;
  98. u16 value;
  99. };
  100. /*
  101. * Values used at the original driver
  102. * Some values are marked as Reserved at the datasheet
  103. */
  104. static const struct i2c_reg_value mt9v011_init_default[] = {
  105. { R0D_MT9V011_RESET, 0x0001 },
  106. { R0D_MT9V011_RESET, 0x0000 },
  107. { R0C_MT9V011_SHUTTER_DELAY, 0x0000 },
  108. { R09_MT9V011_SHUTTER_WIDTH, 0x1fc },
  109. { R0A_MT9V011_CLK_SPEED, 0x0000 },
  110. { R1E_MT9V011_DIGITAL_ZOOM, 0x0000 },
  111. { R20_MT9V011_READ_MODE, 0x1000 },
  112. { R07_MT9V011_OUT_CTRL, 0x0002 }, /* chip enable */
  113. };
  114. static void set_balance(struct v4l2_subdev *sd)
  115. {
  116. struct mt9v011 *core = to_mt9v011(sd);
  117. u16 green1_gain, green2_gain, blue_gain, red_gain;
  118. green1_gain = core->global_gain;
  119. green2_gain = core->global_gain;
  120. blue_gain = core->global_gain +
  121. core->global_gain * core->blue_bal / (1 << 9);
  122. red_gain = core->global_gain +
  123. core->global_gain * core->blue_bal / (1 << 9);
  124. mt9v011_write(sd, R2B_MT9V011_GREEN_1_GAIN, green1_gain);
  125. mt9v011_write(sd, R2E_MT9V011_GREEN_2_GAIN, green1_gain);
  126. mt9v011_write(sd, R2C_MT9V011_BLUE_GAIN, blue_gain);
  127. mt9v011_write(sd, R2D_MT9V011_RED_GAIN, red_gain);
  128. }
  129. static void calc_fps(struct v4l2_subdev *sd, u32 *numerator, u32 *denominator)
  130. {
  131. struct mt9v011 *core = to_mt9v011(sd);
  132. unsigned height, width, hblank, vblank, speed;
  133. unsigned row_time, t_time;
  134. u64 frames_per_ms;
  135. unsigned tmp;
  136. height = mt9v011_read(sd, R03_MT9V011_HEIGHT);
  137. width = mt9v011_read(sd, R04_MT9V011_WIDTH);
  138. hblank = mt9v011_read(sd, R05_MT9V011_HBLANK);
  139. vblank = mt9v011_read(sd, R06_MT9V011_VBLANK);
  140. speed = mt9v011_read(sd, R0A_MT9V011_CLK_SPEED);
  141. row_time = (width + 113 + hblank) * (speed + 2);
  142. t_time = row_time * (height + vblank + 1);
  143. frames_per_ms = core->xtal * 1000l;
  144. do_div(frames_per_ms, t_time);
  145. tmp = frames_per_ms;
  146. v4l2_dbg(1, debug, sd, "Programmed to %u.%03u fps (%d pixel clcks)\n",
  147. tmp / 1000, tmp % 1000, t_time);
  148. if (numerator && denominator) {
  149. *numerator = 1000;
  150. *denominator = (u32)frames_per_ms;
  151. }
  152. }
  153. static u16 calc_speed(struct v4l2_subdev *sd, u32 numerator, u32 denominator)
  154. {
  155. struct mt9v011 *core = to_mt9v011(sd);
  156. unsigned height, width, hblank, vblank;
  157. unsigned row_time, line_time;
  158. u64 t_time, speed;
  159. /* Avoid bogus calculus */
  160. if (!numerator || !denominator)
  161. return 0;
  162. height = mt9v011_read(sd, R03_MT9V011_HEIGHT);
  163. width = mt9v011_read(sd, R04_MT9V011_WIDTH);
  164. hblank = mt9v011_read(sd, R05_MT9V011_HBLANK);
  165. vblank = mt9v011_read(sd, R06_MT9V011_VBLANK);
  166. row_time = width + 113 + hblank;
  167. line_time = height + vblank + 1;
  168. t_time = core->xtal * ((u64)numerator);
  169. /* round to the closest value */
  170. t_time += denominator / 2;
  171. do_div(t_time, denominator);
  172. speed = t_time;
  173. do_div(speed, row_time * line_time);
  174. /* Avoid having a negative value for speed */
  175. if (speed < 2)
  176. speed = 0;
  177. else
  178. speed -= 2;
  179. /* Avoid speed overflow */
  180. if (speed > 15)
  181. return 15;
  182. return (u16)speed;
  183. }
  184. static void set_res(struct v4l2_subdev *sd)
  185. {
  186. struct mt9v011 *core = to_mt9v011(sd);
  187. unsigned vstart, hstart;
  188. /*
  189. * The mt9v011 doesn't have scaling. So, in order to select the desired
  190. * resolution, we're cropping at the middle of the sensor.
  191. * hblank and vblank should be adjusted, in order to warrant that
  192. * we'll preserve the line timings for 30 fps, no matter what resolution
  193. * is selected.
  194. * NOTE: datasheet says that width (and height) should be filled with
  195. * width-1. However, this doesn't work, since one pixel per line will
  196. * be missing.
  197. */
  198. hstart = 14 + (640 - core->width) / 2;
  199. mt9v011_write(sd, R02_MT9V011_COLSTART, hstart);
  200. mt9v011_write(sd, R04_MT9V011_WIDTH, core->width);
  201. mt9v011_write(sd, R05_MT9V011_HBLANK, 771 - core->width);
  202. vstart = 8 + (480 - core->height) / 2;
  203. mt9v011_write(sd, R01_MT9V011_ROWSTART, vstart);
  204. mt9v011_write(sd, R03_MT9V011_HEIGHT, core->height);
  205. mt9v011_write(sd, R06_MT9V011_VBLANK, 508 - core->height);
  206. calc_fps(sd, NULL, NULL);
  207. };
  208. static int mt9v011_reset(struct v4l2_subdev *sd, u32 val)
  209. {
  210. int i;
  211. for (i = 0; i < ARRAY_SIZE(mt9v011_init_default); i++)
  212. mt9v011_write(sd, mt9v011_init_default[i].reg,
  213. mt9v011_init_default[i].value);
  214. set_balance(sd);
  215. set_res(sd);
  216. return 0;
  217. };
  218. static int mt9v011_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  219. {
  220. struct mt9v011 *core = to_mt9v011(sd);
  221. v4l2_dbg(1, debug, sd, "g_ctrl called\n");
  222. switch (ctrl->id) {
  223. case V4L2_CID_GAIN:
  224. ctrl->value = core->global_gain;
  225. return 0;
  226. case V4L2_CID_RED_BALANCE:
  227. ctrl->value = core->red_bal;
  228. return 0;
  229. case V4L2_CID_BLUE_BALANCE:
  230. ctrl->value = core->blue_bal;
  231. return 0;
  232. }
  233. return -EINVAL;
  234. }
  235. static int mt9v011_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
  236. {
  237. int i;
  238. v4l2_dbg(1, debug, sd, "queryctrl called\n");
  239. for (i = 0; i < ARRAY_SIZE(mt9v011_qctrl); i++)
  240. if (qc->id && qc->id == mt9v011_qctrl[i].id) {
  241. memcpy(qc, &(mt9v011_qctrl[i]),
  242. sizeof(*qc));
  243. return 0;
  244. }
  245. return -EINVAL;
  246. }
  247. static int mt9v011_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  248. {
  249. struct mt9v011 *core = to_mt9v011(sd);
  250. u8 i, n;
  251. n = ARRAY_SIZE(mt9v011_qctrl);
  252. for (i = 0; i < n; i++) {
  253. if (ctrl->id != mt9v011_qctrl[i].id)
  254. continue;
  255. if (ctrl->value < mt9v011_qctrl[i].minimum ||
  256. ctrl->value > mt9v011_qctrl[i].maximum)
  257. return -ERANGE;
  258. v4l2_dbg(1, debug, sd, "s_ctrl: id=%d, value=%d\n",
  259. ctrl->id, ctrl->value);
  260. break;
  261. }
  262. switch (ctrl->id) {
  263. case V4L2_CID_GAIN:
  264. core->global_gain = ctrl->value;
  265. break;
  266. case V4L2_CID_RED_BALANCE:
  267. core->red_bal = ctrl->value;
  268. break;
  269. case V4L2_CID_BLUE_BALANCE:
  270. core->blue_bal = ctrl->value;
  271. break;
  272. default:
  273. return -EINVAL;
  274. }
  275. set_balance(sd);
  276. return 0;
  277. }
  278. static int mt9v011_enum_fmt(struct v4l2_subdev *sd, struct v4l2_fmtdesc *fmt)
  279. {
  280. if (fmt->index > 0)
  281. return -EINVAL;
  282. fmt->flags = 0;
  283. strcpy(fmt->description, "8 bpp Bayer GRGR..BGBG");
  284. fmt->pixelformat = V4L2_PIX_FMT_SGRBG8;
  285. return 0;
  286. }
  287. static int mt9v011_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
  288. {
  289. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  290. if (pix->pixelformat != V4L2_PIX_FMT_SGRBG8)
  291. return -EINVAL;
  292. v4l_bound_align_image(&pix->width, 48, 639, 1,
  293. &pix->height, 32, 480, 1, 0);
  294. return 0;
  295. }
  296. static int mt9v011_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
  297. {
  298. struct v4l2_captureparm *cp = &parms->parm.capture;
  299. if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  300. return -EINVAL;
  301. memset(cp, 0, sizeof(struct v4l2_captureparm));
  302. cp->capability = V4L2_CAP_TIMEPERFRAME;
  303. calc_fps(sd,
  304. &cp->timeperframe.numerator,
  305. &cp->timeperframe.denominator);
  306. return 0;
  307. }
  308. static int mt9v011_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
  309. {
  310. struct v4l2_captureparm *cp = &parms->parm.capture;
  311. struct v4l2_fract *tpf = &cp->timeperframe;
  312. u16 speed;
  313. if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  314. return -EINVAL;
  315. if (cp->extendedmode != 0)
  316. return -EINVAL;
  317. speed = calc_speed(sd, tpf->numerator, tpf->denominator);
  318. mt9v011_write(sd, R0A_MT9V011_CLK_SPEED, speed);
  319. v4l2_dbg(1, debug, sd, "Setting speed to %d\n", speed);
  320. /* Recalculate and update fps info */
  321. calc_fps(sd, &tpf->numerator, &tpf->denominator);
  322. return 0;
  323. }
  324. static int mt9v011_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
  325. {
  326. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  327. struct mt9v011 *core = to_mt9v011(sd);
  328. int rc;
  329. rc = mt9v011_try_fmt(sd, fmt);
  330. if (rc < 0)
  331. return -EINVAL;
  332. core->width = pix->width;
  333. core->height = pix->height;
  334. set_res(sd);
  335. return 0;
  336. }
  337. static int mt9v011_s_config(struct v4l2_subdev *sd, int dumb, void *data)
  338. {
  339. struct mt9v011 *core = to_mt9v011(sd);
  340. unsigned *xtal = data;
  341. v4l2_dbg(1, debug, sd, "s_config called\n");
  342. if (xtal) {
  343. core->xtal = *xtal;
  344. v4l2_dbg(1, debug, sd, "xtal set to %d.%03d MHz\n",
  345. *xtal / 1000000, (*xtal / 1000) % 1000);
  346. }
  347. return 0;
  348. }
  349. #ifdef CONFIG_VIDEO_ADV_DEBUG
  350. static int mt9v011_g_register(struct v4l2_subdev *sd,
  351. struct v4l2_dbg_register *reg)
  352. {
  353. struct i2c_client *client = v4l2_get_subdevdata(sd);
  354. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  355. return -EINVAL;
  356. if (!capable(CAP_SYS_ADMIN))
  357. return -EPERM;
  358. reg->val = mt9v011_read(sd, reg->reg & 0xff);
  359. reg->size = 2;
  360. return 0;
  361. }
  362. static int mt9v011_s_register(struct v4l2_subdev *sd,
  363. struct v4l2_dbg_register *reg)
  364. {
  365. struct i2c_client *client = v4l2_get_subdevdata(sd);
  366. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  367. return -EINVAL;
  368. if (!capable(CAP_SYS_ADMIN))
  369. return -EPERM;
  370. mt9v011_write(sd, reg->reg & 0xff, reg->val & 0xffff);
  371. return 0;
  372. }
  373. #endif
  374. static int mt9v011_g_chip_ident(struct v4l2_subdev *sd,
  375. struct v4l2_dbg_chip_ident *chip)
  376. {
  377. u16 version;
  378. struct i2c_client *client = v4l2_get_subdevdata(sd);
  379. version = mt9v011_read(sd, R00_MT9V011_CHIP_VERSION);
  380. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_MT9V011,
  381. version);
  382. }
  383. static const struct v4l2_subdev_core_ops mt9v011_core_ops = {
  384. .queryctrl = mt9v011_queryctrl,
  385. .g_ctrl = mt9v011_g_ctrl,
  386. .s_ctrl = mt9v011_s_ctrl,
  387. .reset = mt9v011_reset,
  388. .s_config = mt9v011_s_config,
  389. .g_chip_ident = mt9v011_g_chip_ident,
  390. #ifdef CONFIG_VIDEO_ADV_DEBUG
  391. .g_register = mt9v011_g_register,
  392. .s_register = mt9v011_s_register,
  393. #endif
  394. };
  395. static const struct v4l2_subdev_video_ops mt9v011_video_ops = {
  396. .enum_fmt = mt9v011_enum_fmt,
  397. .try_fmt = mt9v011_try_fmt,
  398. .s_fmt = mt9v011_s_fmt,
  399. .g_parm = mt9v011_g_parm,
  400. .s_parm = mt9v011_s_parm,
  401. };
  402. static const struct v4l2_subdev_ops mt9v011_ops = {
  403. .core = &mt9v011_core_ops,
  404. .video = &mt9v011_video_ops,
  405. };
  406. /****************************************************************************
  407. I2C Client & Driver
  408. ****************************************************************************/
  409. static int mt9v011_probe(struct i2c_client *c,
  410. const struct i2c_device_id *id)
  411. {
  412. u16 version;
  413. struct mt9v011 *core;
  414. struct v4l2_subdev *sd;
  415. /* Check if the adapter supports the needed features */
  416. if (!i2c_check_functionality(c->adapter,
  417. I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  418. return -EIO;
  419. core = kzalloc(sizeof(struct mt9v011), GFP_KERNEL);
  420. if (!core)
  421. return -ENOMEM;
  422. sd = &core->sd;
  423. v4l2_i2c_subdev_init(sd, c, &mt9v011_ops);
  424. /* Check if the sensor is really a MT9V011 */
  425. version = mt9v011_read(sd, R00_MT9V011_CHIP_VERSION);
  426. if ((version != MT9V011_VERSION) &&
  427. (version != MT9V011_REV_B_VERSION)) {
  428. v4l2_info(sd, "*** unknown micron chip detected (0x%04x).\n",
  429. version);
  430. kfree(core);
  431. return -EINVAL;
  432. }
  433. core->global_gain = 0x0024;
  434. core->width = 640;
  435. core->height = 480;
  436. core->xtal = 27000000; /* Hz */
  437. v4l_info(c, "chip found @ 0x%02x (%s - chip version 0x%04x)\n",
  438. c->addr << 1, c->adapter->name, version);
  439. return 0;
  440. }
  441. static int mt9v011_remove(struct i2c_client *c)
  442. {
  443. struct v4l2_subdev *sd = i2c_get_clientdata(c);
  444. v4l2_dbg(1, debug, sd,
  445. "mt9v011.c: removing mt9v011 adapter on address 0x%x\n",
  446. c->addr << 1);
  447. v4l2_device_unregister_subdev(sd);
  448. kfree(to_mt9v011(sd));
  449. return 0;
  450. }
  451. /* ----------------------------------------------------------------------- */
  452. static const struct i2c_device_id mt9v011_id[] = {
  453. { "mt9v011", 0 },
  454. { }
  455. };
  456. MODULE_DEVICE_TABLE(i2c, mt9v011_id);
  457. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  458. .name = "mt9v011",
  459. .probe = mt9v011_probe,
  460. .remove = mt9v011_remove,
  461. .id_table = mt9v011_id,
  462. };