mt9v011.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 <media/v4l2-device.h>
  11. #include "mt9v011.h"
  12. #include <media/v4l2-i2c-drv.h>
  13. #include <media/v4l2-chip-ident.h>
  14. MODULE_DESCRIPTION("Micron mt9v011 sensor driver");
  15. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  16. MODULE_LICENSE("GPL");
  17. static int debug;
  18. module_param(debug, int, 0);
  19. MODULE_PARM_DESC(debug, "Debug level (0-2)");
  20. /* supported controls */
  21. static struct v4l2_queryctrl mt9v011_qctrl[] = {
  22. {
  23. .id = V4L2_CID_GAIN,
  24. .type = V4L2_CTRL_TYPE_INTEGER,
  25. .name = "Gain",
  26. .minimum = 0,
  27. .maximum = (1 << 10) - 1,
  28. .step = 1,
  29. .default_value = 0x0020,
  30. .flags = 0,
  31. }, {
  32. .id = V4L2_CID_RED_BALANCE,
  33. .type = V4L2_CTRL_TYPE_INTEGER,
  34. .name = "Red Balance",
  35. .minimum = -1 << 9,
  36. .maximum = (1 << 9) - 1,
  37. .step = 1,
  38. .default_value = 0,
  39. .flags = 0,
  40. }, {
  41. .id = V4L2_CID_BLUE_BALANCE,
  42. .type = V4L2_CTRL_TYPE_INTEGER,
  43. .name = "Blue Balance",
  44. .minimum = -1 << 9,
  45. .maximum = (1 << 9) - 1,
  46. .step = 1,
  47. .default_value = 0,
  48. .flags = 0,
  49. },
  50. };
  51. struct mt9v011 {
  52. struct v4l2_subdev sd;
  53. unsigned width, height;
  54. u16 global_gain, red_bal, blue_bal;
  55. };
  56. static inline struct mt9v011 *to_mt9v011(struct v4l2_subdev *sd)
  57. {
  58. return container_of(sd, struct mt9v011, sd);
  59. }
  60. static int mt9v011_read(struct v4l2_subdev *sd, unsigned char addr)
  61. {
  62. struct i2c_client *c = v4l2_get_subdevdata(sd);
  63. __be16 buffer;
  64. int rc, val;
  65. rc = i2c_master_send(c, &addr, 1);
  66. if (rc != 1)
  67. v4l2_dbg(0, debug, sd,
  68. "i2c i/o error: rc == %d (should be 1)\n", rc);
  69. msleep(10);
  70. rc = i2c_master_recv(c, (char *)&buffer, 2);
  71. if (rc != 2)
  72. v4l2_dbg(0, debug, sd,
  73. "i2c i/o error: rc == %d (should be 2)\n", rc);
  74. val = be16_to_cpu(buffer);
  75. v4l2_dbg(2, debug, sd, "mt9v011: read 0x%02x = 0x%04x\n", addr, val);
  76. return val;
  77. }
  78. static void mt9v011_write(struct v4l2_subdev *sd, unsigned char addr,
  79. u16 value)
  80. {
  81. struct i2c_client *c = v4l2_get_subdevdata(sd);
  82. unsigned char buffer[3];
  83. int rc;
  84. buffer[0] = addr;
  85. buffer[1] = value >> 8;
  86. buffer[2] = value & 0xff;
  87. v4l2_dbg(2, debug, sd,
  88. "mt9v011: writing 0x%02x 0x%04x\n", buffer[0], value);
  89. rc = i2c_master_send(c, buffer, 3);
  90. if (rc != 3)
  91. v4l2_dbg(0, debug, sd,
  92. "i2c i/o error: rc == %d (should be 3)\n", rc);
  93. }
  94. struct i2c_reg_value {
  95. unsigned char reg;
  96. u16 value;
  97. };
  98. /*
  99. * Values used at the original driver
  100. * Some values are marked as Reserved at the datasheet
  101. */
  102. static const struct i2c_reg_value mt9v011_init_default[] = {
  103. { R0D_MT9V011_RESET, 0x0001 },
  104. { R0D_MT9V011_RESET, 0x0000 },
  105. { R0C_MT9V011_SHUTTER_DELAY, 0x0000 },
  106. { R09_MT9V011_SHUTTER_WIDTH, 0x1fc },
  107. { R0A_MT9V011_CLK_SPEED, 0x0000 },
  108. { R1E_MT9V011_DIGITAL_ZOOM, 0x0000 },
  109. { R20_MT9V011_READ_MODE, 0x1000 },
  110. { R07_MT9V011_OUT_CTRL, 0x000a }, /* chip enable */
  111. };
  112. static void set_balance(struct v4l2_subdev *sd)
  113. {
  114. struct mt9v011 *core = to_mt9v011(sd);
  115. u16 green1_gain, green2_gain, blue_gain, red_gain;
  116. green1_gain = core->global_gain;
  117. green2_gain = core->global_gain;
  118. blue_gain = core->global_gain +
  119. core->global_gain * core->blue_bal / (1 << 9);
  120. red_gain = core->global_gain +
  121. core->global_gain * core->blue_bal / (1 << 9);
  122. mt9v011_write(sd, R2B_MT9V011_GREEN_1_GAIN, green1_gain);
  123. mt9v011_write(sd, R2E_MT9V011_GREEN_2_GAIN, green1_gain);
  124. mt9v011_write(sd, R2C_MT9V011_BLUE_GAIN, blue_gain);
  125. mt9v011_write(sd, R2D_MT9V011_RED_GAIN, red_gain);
  126. }
  127. static void set_res(struct v4l2_subdev *sd)
  128. {
  129. struct mt9v011 *core = to_mt9v011(sd);
  130. unsigned vstart, hstart;
  131. /*
  132. * The mt9v011 doesn't have scaling. So, in order to select the desired
  133. * resolution, we're cropping at the middle of the sensor.
  134. * hblank and vblank should be adjusted, in order to warrant that
  135. * we'll preserve the line timings for 30 fps, no matter what resolution
  136. * is selected.
  137. * NOTE: datasheet says that width (and height) should be filled with
  138. * width-1. However, this doesn't work, since one pixel per line will
  139. * be missing.
  140. */
  141. hstart = 14 + (640 - core->width) / 2;
  142. mt9v011_write(sd, R02_MT9V011_COLSTART, hstart);
  143. mt9v011_write(sd, R04_MT9V011_WIDTH, core->width);
  144. mt9v011_write(sd, R05_MT9V011_HBLANK, 771 - core->width);
  145. vstart = 8 + (640 - core->height) / 2;
  146. mt9v011_write(sd, R01_MT9V011_ROWSTART, vstart);
  147. mt9v011_write(sd, R03_MT9V011_HEIGHT, core->height);
  148. mt9v011_write(sd, R06_MT9V011_VBLANK, 508 - core->height);
  149. };
  150. static int mt9v011_reset(struct v4l2_subdev *sd, u32 val)
  151. {
  152. int i;
  153. for (i = 0; i < ARRAY_SIZE(mt9v011_init_default); i++)
  154. mt9v011_write(sd, mt9v011_init_default[i].reg,
  155. mt9v011_init_default[i].value);
  156. set_balance(sd);
  157. set_res(sd);
  158. return 0;
  159. };
  160. static int mt9v011_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  161. {
  162. struct mt9v011 *core = to_mt9v011(sd);
  163. v4l2_dbg(1, debug, sd, "g_ctrl called\n");
  164. switch (ctrl->id) {
  165. case V4L2_CID_GAIN:
  166. ctrl->value = core->global_gain;
  167. return 0;
  168. case V4L2_CID_RED_BALANCE:
  169. ctrl->value = core->red_bal;
  170. return 0;
  171. case V4L2_CID_BLUE_BALANCE:
  172. ctrl->value = core->blue_bal;
  173. return 0;
  174. }
  175. return -EINVAL;
  176. }
  177. static int mt9v011_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  178. {
  179. struct mt9v011 *core = to_mt9v011(sd);
  180. u8 i, n;
  181. n = ARRAY_SIZE(mt9v011_qctrl);
  182. for (i = 0; i < n; i++) {
  183. if (ctrl->id != mt9v011_qctrl[i].id)
  184. continue;
  185. if (ctrl->value < mt9v011_qctrl[i].minimum ||
  186. ctrl->value > mt9v011_qctrl[i].maximum)
  187. return -ERANGE;
  188. v4l2_dbg(1, debug, sd, "s_ctrl: id=%d, value=%d\n",
  189. ctrl->id, ctrl->value);
  190. break;
  191. }
  192. switch (ctrl->id) {
  193. case V4L2_CID_GAIN:
  194. core->global_gain = ctrl->value;
  195. break;
  196. case V4L2_CID_RED_BALANCE:
  197. core->red_bal = ctrl->value;
  198. break;
  199. case V4L2_CID_BLUE_BALANCE:
  200. core->blue_bal = ctrl->value;
  201. break;
  202. default:
  203. return -EINVAL;
  204. }
  205. set_balance(sd);
  206. return 0;
  207. }
  208. static int mt9v011_enum_fmt(struct v4l2_subdev *sd, struct v4l2_fmtdesc *fmt)
  209. {
  210. if (fmt->index > 0)
  211. return -EINVAL;
  212. fmt->flags = 0;
  213. strcpy(fmt->description, "8 bpp Bayer GRGR..BGBG");
  214. fmt->pixelformat = V4L2_PIX_FMT_SGRBG8;
  215. return 0;
  216. }
  217. static int mt9v011_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
  218. {
  219. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  220. if (pix->pixelformat != V4L2_PIX_FMT_SGRBG8)
  221. return -EINVAL;
  222. v4l_bound_align_image(&pix->width, 48, 639, 1,
  223. &pix->height, 32, 480, 1, 0);
  224. return 0;
  225. }
  226. static int mt9v011_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
  227. {
  228. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  229. struct mt9v011 *core = to_mt9v011(sd);
  230. int rc;
  231. rc = mt9v011_try_fmt(sd, fmt);
  232. if (rc < 0)
  233. return -EINVAL;
  234. core->width = pix->width;
  235. core->height = pix->height;
  236. set_res(sd);
  237. return 0;
  238. }
  239. #ifdef CONFIG_VIDEO_ADV_DEBUG
  240. static int mt9v011_g_register(struct v4l2_subdev *sd,
  241. struct v4l2_dbg_register *reg)
  242. {
  243. struct i2c_client *client = v4l2_get_subdevdata(sd);
  244. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  245. return -EINVAL;
  246. if (!capable(CAP_SYS_ADMIN))
  247. return -EPERM;
  248. reg->val = mt9v011_read(sd, reg->reg & 0xff);
  249. reg->size = 2;
  250. return 0;
  251. }
  252. static int mt9v011_s_register(struct v4l2_subdev *sd,
  253. struct v4l2_dbg_register *reg)
  254. {
  255. struct i2c_client *client = v4l2_get_subdevdata(sd);
  256. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  257. return -EINVAL;
  258. if (!capable(CAP_SYS_ADMIN))
  259. return -EPERM;
  260. mt9v011_write(sd, reg->reg & 0xff, reg->val & 0xffff);
  261. return 0;
  262. }
  263. #endif
  264. static int mt9v011_g_chip_ident(struct v4l2_subdev *sd,
  265. struct v4l2_dbg_chip_ident *chip)
  266. {
  267. struct i2c_client *client = v4l2_get_subdevdata(sd);
  268. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_MT9V011,
  269. MT9V011_VERSION);
  270. }
  271. static const struct v4l2_subdev_core_ops mt9v011_core_ops = {
  272. .g_ctrl = mt9v011_g_ctrl,
  273. .s_ctrl = mt9v011_s_ctrl,
  274. .reset = mt9v011_reset,
  275. .g_chip_ident = mt9v011_g_chip_ident,
  276. #ifdef CONFIG_VIDEO_ADV_DEBUG
  277. .g_register = mt9v011_g_register,
  278. .s_register = mt9v011_s_register,
  279. #endif
  280. };
  281. static const struct v4l2_subdev_video_ops mt9v011_video_ops = {
  282. .enum_fmt = mt9v011_enum_fmt,
  283. .try_fmt = mt9v011_try_fmt,
  284. .s_fmt = mt9v011_s_fmt,
  285. };
  286. static const struct v4l2_subdev_ops mt9v011_ops = {
  287. .core = &mt9v011_core_ops,
  288. .video = &mt9v011_video_ops,
  289. };
  290. /****************************************************************************
  291. I2C Client & Driver
  292. ****************************************************************************/
  293. static int mt9v011_probe(struct i2c_client *c,
  294. const struct i2c_device_id *id)
  295. {
  296. u16 version;
  297. struct mt9v011 *core;
  298. struct v4l2_subdev *sd;
  299. /* Check if the adapter supports the needed features */
  300. if (!i2c_check_functionality(c->adapter,
  301. I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  302. return -EIO;
  303. core = kzalloc(sizeof(struct mt9v011), GFP_KERNEL);
  304. if (!core)
  305. return -ENOMEM;
  306. sd = &core->sd;
  307. v4l2_i2c_subdev_init(sd, c, &mt9v011_ops);
  308. /* Check if the sensor is really a MT9V011 */
  309. version = mt9v011_read(sd, R00_MT9V011_CHIP_VERSION);
  310. if (version != MT9V011_VERSION) {
  311. v4l2_info(sd, "*** unknown micron chip detected (0x%04x.\n",
  312. version);
  313. kfree(core);
  314. return -EINVAL;
  315. }
  316. core->global_gain = 0x0024;
  317. core->width = 640;
  318. core->height = 480;
  319. v4l_info(c, "chip found @ 0x%02x (%s)\n",
  320. c->addr << 1, c->adapter->name);
  321. return 0;
  322. }
  323. static int mt9v011_remove(struct i2c_client *c)
  324. {
  325. struct v4l2_subdev *sd = i2c_get_clientdata(c);
  326. v4l2_dbg(1, debug, sd,
  327. "mt9v011.c: removing mt9v011 adapter on address 0x%x\n",
  328. c->addr << 1);
  329. v4l2_device_unregister_subdev(sd);
  330. kfree(to_mt9v011(sd));
  331. return 0;
  332. }
  333. /* ----------------------------------------------------------------------- */
  334. static const struct i2c_device_id mt9v011_id[] = {
  335. { "mt9v011", 0 },
  336. { }
  337. };
  338. MODULE_DEVICE_TABLE(i2c, mt9v011_id);
  339. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  340. .name = "mt9v011",
  341. .probe = mt9v011_probe,
  342. .remove = mt9v011_remove,
  343. .id_table = mt9v011_id,
  344. };