mt9m001.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /*
  2. * Driver for MT9M001 CMOS Image Sensor from Micron
  3. *
  4. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.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 <linux/module.h>
  15. #include <media/soc_camera.h>
  16. #include <media/soc_mediabus.h>
  17. #include <media/v4l2-clk.h>
  18. #include <media/v4l2-subdev.h>
  19. #include <media/v4l2-ctrls.h>
  20. /*
  21. * mt9m001 i2c address 0x5d
  22. * The platform has to define struct i2c_board_info objects and link to them
  23. * from struct soc_camera_host_desc
  24. */
  25. /* mt9m001 selected register addresses */
  26. #define MT9M001_CHIP_VERSION 0x00
  27. #define MT9M001_ROW_START 0x01
  28. #define MT9M001_COLUMN_START 0x02
  29. #define MT9M001_WINDOW_HEIGHT 0x03
  30. #define MT9M001_WINDOW_WIDTH 0x04
  31. #define MT9M001_HORIZONTAL_BLANKING 0x05
  32. #define MT9M001_VERTICAL_BLANKING 0x06
  33. #define MT9M001_OUTPUT_CONTROL 0x07
  34. #define MT9M001_SHUTTER_WIDTH 0x09
  35. #define MT9M001_FRAME_RESTART 0x0b
  36. #define MT9M001_SHUTTER_DELAY 0x0c
  37. #define MT9M001_RESET 0x0d
  38. #define MT9M001_READ_OPTIONS1 0x1e
  39. #define MT9M001_READ_OPTIONS2 0x20
  40. #define MT9M001_GLOBAL_GAIN 0x35
  41. #define MT9M001_CHIP_ENABLE 0xF1
  42. #define MT9M001_MAX_WIDTH 1280
  43. #define MT9M001_MAX_HEIGHT 1024
  44. #define MT9M001_MIN_WIDTH 48
  45. #define MT9M001_MIN_HEIGHT 32
  46. #define MT9M001_COLUMN_SKIP 20
  47. #define MT9M001_ROW_SKIP 12
  48. /* MT9M001 has only one fixed colorspace per pixelcode */
  49. struct mt9m001_datafmt {
  50. u32 code;
  51. enum v4l2_colorspace colorspace;
  52. };
  53. /* Find a data format by a pixel code in an array */
  54. static const struct mt9m001_datafmt *mt9m001_find_datafmt(
  55. u32 code, const struct mt9m001_datafmt *fmt,
  56. int n)
  57. {
  58. int i;
  59. for (i = 0; i < n; i++)
  60. if (fmt[i].code == code)
  61. return fmt + i;
  62. return NULL;
  63. }
  64. static const struct mt9m001_datafmt mt9m001_colour_fmts[] = {
  65. /*
  66. * Order important: first natively supported,
  67. * second supported with a GPIO extender
  68. */
  69. {MEDIA_BUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
  70. {MEDIA_BUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
  71. };
  72. static const struct mt9m001_datafmt mt9m001_monochrome_fmts[] = {
  73. /* Order important - see above */
  74. {MEDIA_BUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
  75. {MEDIA_BUS_FMT_Y8_1X8, V4L2_COLORSPACE_JPEG},
  76. };
  77. struct mt9m001 {
  78. struct v4l2_subdev subdev;
  79. struct v4l2_ctrl_handler hdl;
  80. struct {
  81. /* exposure/auto-exposure cluster */
  82. struct v4l2_ctrl *autoexposure;
  83. struct v4l2_ctrl *exposure;
  84. };
  85. struct v4l2_rect rect; /* Sensor window */
  86. struct v4l2_clk *clk;
  87. const struct mt9m001_datafmt *fmt;
  88. const struct mt9m001_datafmt *fmts;
  89. int num_fmts;
  90. unsigned int total_h;
  91. unsigned short y_skip_top; /* Lines to skip at the top */
  92. };
  93. static struct mt9m001 *to_mt9m001(const struct i2c_client *client)
  94. {
  95. return container_of(i2c_get_clientdata(client), struct mt9m001, subdev);
  96. }
  97. static int reg_read(struct i2c_client *client, const u8 reg)
  98. {
  99. return i2c_smbus_read_word_swapped(client, reg);
  100. }
  101. static int reg_write(struct i2c_client *client, const u8 reg,
  102. const u16 data)
  103. {
  104. return i2c_smbus_write_word_swapped(client, reg, data);
  105. }
  106. static int reg_set(struct i2c_client *client, const u8 reg,
  107. const u16 data)
  108. {
  109. int ret;
  110. ret = reg_read(client, reg);
  111. if (ret < 0)
  112. return ret;
  113. return reg_write(client, reg, ret | data);
  114. }
  115. static int reg_clear(struct i2c_client *client, const u8 reg,
  116. const u16 data)
  117. {
  118. int ret;
  119. ret = reg_read(client, reg);
  120. if (ret < 0)
  121. return ret;
  122. return reg_write(client, reg, ret & ~data);
  123. }
  124. static int mt9m001_init(struct i2c_client *client)
  125. {
  126. int ret;
  127. dev_dbg(&client->dev, "%s\n", __func__);
  128. /*
  129. * We don't know, whether platform provides reset, issue a soft reset
  130. * too. This returns all registers to their default values.
  131. */
  132. ret = reg_write(client, MT9M001_RESET, 1);
  133. if (!ret)
  134. ret = reg_write(client, MT9M001_RESET, 0);
  135. /* Disable chip, synchronous option update */
  136. if (!ret)
  137. ret = reg_write(client, MT9M001_OUTPUT_CONTROL, 0);
  138. return ret;
  139. }
  140. static int mt9m001_s_stream(struct v4l2_subdev *sd, int enable)
  141. {
  142. struct i2c_client *client = v4l2_get_subdevdata(sd);
  143. /* Switch to master "normal" mode or stop sensor readout */
  144. if (reg_write(client, MT9M001_OUTPUT_CONTROL, enable ? 2 : 0) < 0)
  145. return -EIO;
  146. return 0;
  147. }
  148. static int mt9m001_s_crop(struct v4l2_subdev *sd, const struct v4l2_crop *a)
  149. {
  150. struct i2c_client *client = v4l2_get_subdevdata(sd);
  151. struct mt9m001 *mt9m001 = to_mt9m001(client);
  152. struct v4l2_rect rect = a->c;
  153. int ret;
  154. const u16 hblank = 9, vblank = 25;
  155. if (mt9m001->fmts == mt9m001_colour_fmts)
  156. /*
  157. * Bayer format - even number of rows for simplicity,
  158. * but let the user play with the top row.
  159. */
  160. rect.height = ALIGN(rect.height, 2);
  161. /* Datasheet requirement: see register description */
  162. rect.width = ALIGN(rect.width, 2);
  163. rect.left = ALIGN(rect.left, 2);
  164. soc_camera_limit_side(&rect.left, &rect.width,
  165. MT9M001_COLUMN_SKIP, MT9M001_MIN_WIDTH, MT9M001_MAX_WIDTH);
  166. soc_camera_limit_side(&rect.top, &rect.height,
  167. MT9M001_ROW_SKIP, MT9M001_MIN_HEIGHT, MT9M001_MAX_HEIGHT);
  168. mt9m001->total_h = rect.height + mt9m001->y_skip_top + vblank;
  169. /* Blanking and start values - default... */
  170. ret = reg_write(client, MT9M001_HORIZONTAL_BLANKING, hblank);
  171. if (!ret)
  172. ret = reg_write(client, MT9M001_VERTICAL_BLANKING, vblank);
  173. /*
  174. * The caller provides a supported format, as verified per
  175. * call to .set_fmt(FORMAT_TRY).
  176. */
  177. if (!ret)
  178. ret = reg_write(client, MT9M001_COLUMN_START, rect.left);
  179. if (!ret)
  180. ret = reg_write(client, MT9M001_ROW_START, rect.top);
  181. if (!ret)
  182. ret = reg_write(client, MT9M001_WINDOW_WIDTH, rect.width - 1);
  183. if (!ret)
  184. ret = reg_write(client, MT9M001_WINDOW_HEIGHT,
  185. rect.height + mt9m001->y_skip_top - 1);
  186. if (!ret && v4l2_ctrl_g_ctrl(mt9m001->autoexposure) == V4L2_EXPOSURE_AUTO)
  187. ret = reg_write(client, MT9M001_SHUTTER_WIDTH, mt9m001->total_h);
  188. if (!ret)
  189. mt9m001->rect = rect;
  190. return ret;
  191. }
  192. static int mt9m001_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  193. {
  194. struct i2c_client *client = v4l2_get_subdevdata(sd);
  195. struct mt9m001 *mt9m001 = to_mt9m001(client);
  196. a->c = mt9m001->rect;
  197. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  198. return 0;
  199. }
  200. static int mt9m001_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  201. {
  202. a->bounds.left = MT9M001_COLUMN_SKIP;
  203. a->bounds.top = MT9M001_ROW_SKIP;
  204. a->bounds.width = MT9M001_MAX_WIDTH;
  205. a->bounds.height = MT9M001_MAX_HEIGHT;
  206. a->defrect = a->bounds;
  207. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  208. a->pixelaspect.numerator = 1;
  209. a->pixelaspect.denominator = 1;
  210. return 0;
  211. }
  212. static int mt9m001_get_fmt(struct v4l2_subdev *sd,
  213. struct v4l2_subdev_pad_config *cfg,
  214. struct v4l2_subdev_format *format)
  215. {
  216. struct i2c_client *client = v4l2_get_subdevdata(sd);
  217. struct mt9m001 *mt9m001 = to_mt9m001(client);
  218. struct v4l2_mbus_framefmt *mf = &format->format;
  219. if (format->pad)
  220. return -EINVAL;
  221. mf->width = mt9m001->rect.width;
  222. mf->height = mt9m001->rect.height;
  223. mf->code = mt9m001->fmt->code;
  224. mf->colorspace = mt9m001->fmt->colorspace;
  225. mf->field = V4L2_FIELD_NONE;
  226. return 0;
  227. }
  228. static int mt9m001_s_fmt(struct v4l2_subdev *sd,
  229. struct v4l2_mbus_framefmt *mf)
  230. {
  231. struct i2c_client *client = v4l2_get_subdevdata(sd);
  232. struct mt9m001 *mt9m001 = to_mt9m001(client);
  233. struct v4l2_crop a = {
  234. .c = {
  235. .left = mt9m001->rect.left,
  236. .top = mt9m001->rect.top,
  237. .width = mf->width,
  238. .height = mf->height,
  239. },
  240. };
  241. int ret;
  242. /* No support for scaling so far, just crop. TODO: use skipping */
  243. ret = mt9m001_s_crop(sd, &a);
  244. if (!ret) {
  245. mf->width = mt9m001->rect.width;
  246. mf->height = mt9m001->rect.height;
  247. mt9m001->fmt = mt9m001_find_datafmt(mf->code,
  248. mt9m001->fmts, mt9m001->num_fmts);
  249. mf->colorspace = mt9m001->fmt->colorspace;
  250. }
  251. return ret;
  252. }
  253. static int mt9m001_set_fmt(struct v4l2_subdev *sd,
  254. struct v4l2_subdev_pad_config *cfg,
  255. struct v4l2_subdev_format *format)
  256. {
  257. struct v4l2_mbus_framefmt *mf = &format->format;
  258. struct i2c_client *client = v4l2_get_subdevdata(sd);
  259. struct mt9m001 *mt9m001 = to_mt9m001(client);
  260. const struct mt9m001_datafmt *fmt;
  261. if (format->pad)
  262. return -EINVAL;
  263. v4l_bound_align_image(&mf->width, MT9M001_MIN_WIDTH,
  264. MT9M001_MAX_WIDTH, 1,
  265. &mf->height, MT9M001_MIN_HEIGHT + mt9m001->y_skip_top,
  266. MT9M001_MAX_HEIGHT + mt9m001->y_skip_top, 0, 0);
  267. if (mt9m001->fmts == mt9m001_colour_fmts)
  268. mf->height = ALIGN(mf->height - 1, 2);
  269. fmt = mt9m001_find_datafmt(mf->code, mt9m001->fmts,
  270. mt9m001->num_fmts);
  271. if (!fmt) {
  272. fmt = mt9m001->fmt;
  273. mf->code = fmt->code;
  274. }
  275. mf->colorspace = fmt->colorspace;
  276. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  277. return mt9m001_s_fmt(sd, mf);
  278. cfg->try_fmt = *mf;
  279. return 0;
  280. }
  281. #ifdef CONFIG_VIDEO_ADV_DEBUG
  282. static int mt9m001_g_register(struct v4l2_subdev *sd,
  283. struct v4l2_dbg_register *reg)
  284. {
  285. struct i2c_client *client = v4l2_get_subdevdata(sd);
  286. if (reg->reg > 0xff)
  287. return -EINVAL;
  288. reg->size = 2;
  289. reg->val = reg_read(client, reg->reg);
  290. if (reg->val > 0xffff)
  291. return -EIO;
  292. return 0;
  293. }
  294. static int mt9m001_s_register(struct v4l2_subdev *sd,
  295. const struct v4l2_dbg_register *reg)
  296. {
  297. struct i2c_client *client = v4l2_get_subdevdata(sd);
  298. if (reg->reg > 0xff)
  299. return -EINVAL;
  300. if (reg_write(client, reg->reg, reg->val) < 0)
  301. return -EIO;
  302. return 0;
  303. }
  304. #endif
  305. static int mt9m001_s_power(struct v4l2_subdev *sd, int on)
  306. {
  307. struct i2c_client *client = v4l2_get_subdevdata(sd);
  308. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  309. struct mt9m001 *mt9m001 = to_mt9m001(client);
  310. return soc_camera_set_power(&client->dev, ssdd, mt9m001->clk, on);
  311. }
  312. static int mt9m001_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  313. {
  314. struct mt9m001 *mt9m001 = container_of(ctrl->handler,
  315. struct mt9m001, hdl);
  316. s32 min, max;
  317. switch (ctrl->id) {
  318. case V4L2_CID_EXPOSURE_AUTO:
  319. min = mt9m001->exposure->minimum;
  320. max = mt9m001->exposure->maximum;
  321. mt9m001->exposure->val =
  322. (524 + (mt9m001->total_h - 1) * (max - min)) / 1048 + min;
  323. break;
  324. }
  325. return 0;
  326. }
  327. static int mt9m001_s_ctrl(struct v4l2_ctrl *ctrl)
  328. {
  329. struct mt9m001 *mt9m001 = container_of(ctrl->handler,
  330. struct mt9m001, hdl);
  331. struct v4l2_subdev *sd = &mt9m001->subdev;
  332. struct i2c_client *client = v4l2_get_subdevdata(sd);
  333. struct v4l2_ctrl *exp = mt9m001->exposure;
  334. int data;
  335. switch (ctrl->id) {
  336. case V4L2_CID_VFLIP:
  337. if (ctrl->val)
  338. data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000);
  339. else
  340. data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000);
  341. if (data < 0)
  342. return -EIO;
  343. return 0;
  344. case V4L2_CID_GAIN:
  345. /* See Datasheet Table 7, Gain settings. */
  346. if (ctrl->val <= ctrl->default_value) {
  347. /* Pack it into 0..1 step 0.125, register values 0..8 */
  348. unsigned long range = ctrl->default_value - ctrl->minimum;
  349. data = ((ctrl->val - (s32)ctrl->minimum) * 8 + range / 2) / range;
  350. dev_dbg(&client->dev, "Setting gain %d\n", data);
  351. data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
  352. if (data < 0)
  353. return -EIO;
  354. } else {
  355. /* Pack it into 1.125..15 variable step, register values 9..67 */
  356. /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
  357. unsigned long range = ctrl->maximum - ctrl->default_value - 1;
  358. unsigned long gain = ((ctrl->val - (s32)ctrl->default_value - 1) *
  359. 111 + range / 2) / range + 9;
  360. if (gain <= 32)
  361. data = gain;
  362. else if (gain <= 64)
  363. data = ((gain - 32) * 16 + 16) / 32 + 80;
  364. else
  365. data = ((gain - 64) * 7 + 28) / 56 + 96;
  366. dev_dbg(&client->dev, "Setting gain from %d to %d\n",
  367. reg_read(client, MT9M001_GLOBAL_GAIN), data);
  368. data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
  369. if (data < 0)
  370. return -EIO;
  371. }
  372. return 0;
  373. case V4L2_CID_EXPOSURE_AUTO:
  374. if (ctrl->val == V4L2_EXPOSURE_MANUAL) {
  375. unsigned long range = exp->maximum - exp->minimum;
  376. unsigned long shutter = ((exp->val - (s32)exp->minimum) * 1048 +
  377. range / 2) / range + 1;
  378. dev_dbg(&client->dev,
  379. "Setting shutter width from %d to %lu\n",
  380. reg_read(client, MT9M001_SHUTTER_WIDTH), shutter);
  381. if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0)
  382. return -EIO;
  383. } else {
  384. const u16 vblank = 25;
  385. mt9m001->total_h = mt9m001->rect.height +
  386. mt9m001->y_skip_top + vblank;
  387. if (reg_write(client, MT9M001_SHUTTER_WIDTH, mt9m001->total_h) < 0)
  388. return -EIO;
  389. }
  390. return 0;
  391. }
  392. return -EINVAL;
  393. }
  394. /*
  395. * Interface active, can use i2c. If it fails, it can indeed mean, that
  396. * this wasn't our capture interface, so, we wait for the right one
  397. */
  398. static int mt9m001_video_probe(struct soc_camera_subdev_desc *ssdd,
  399. struct i2c_client *client)
  400. {
  401. struct mt9m001 *mt9m001 = to_mt9m001(client);
  402. s32 data;
  403. unsigned long flags;
  404. int ret;
  405. ret = mt9m001_s_power(&mt9m001->subdev, 1);
  406. if (ret < 0)
  407. return ret;
  408. /* Enable the chip */
  409. data = reg_write(client, MT9M001_CHIP_ENABLE, 1);
  410. dev_dbg(&client->dev, "write: %d\n", data);
  411. /* Read out the chip version register */
  412. data = reg_read(client, MT9M001_CHIP_VERSION);
  413. /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
  414. switch (data) {
  415. case 0x8411:
  416. case 0x8421:
  417. mt9m001->fmts = mt9m001_colour_fmts;
  418. break;
  419. case 0x8431:
  420. mt9m001->fmts = mt9m001_monochrome_fmts;
  421. break;
  422. default:
  423. dev_err(&client->dev,
  424. "No MT9M001 chip detected, register read %x\n", data);
  425. ret = -ENODEV;
  426. goto done;
  427. }
  428. mt9m001->num_fmts = 0;
  429. /*
  430. * This is a 10bit sensor, so by default we only allow 10bit.
  431. * The platform may support different bus widths due to
  432. * different routing of the data lines.
  433. */
  434. if (ssdd->query_bus_param)
  435. flags = ssdd->query_bus_param(ssdd);
  436. else
  437. flags = SOCAM_DATAWIDTH_10;
  438. if (flags & SOCAM_DATAWIDTH_10)
  439. mt9m001->num_fmts++;
  440. else
  441. mt9m001->fmts++;
  442. if (flags & SOCAM_DATAWIDTH_8)
  443. mt9m001->num_fmts++;
  444. mt9m001->fmt = &mt9m001->fmts[0];
  445. dev_info(&client->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
  446. data == 0x8431 ? "C12STM" : "C12ST");
  447. ret = mt9m001_init(client);
  448. if (ret < 0) {
  449. dev_err(&client->dev, "Failed to initialise the camera\n");
  450. goto done;
  451. }
  452. /* mt9m001_init() has reset the chip, returning registers to defaults */
  453. ret = v4l2_ctrl_handler_setup(&mt9m001->hdl);
  454. done:
  455. mt9m001_s_power(&mt9m001->subdev, 0);
  456. return ret;
  457. }
  458. static void mt9m001_video_remove(struct soc_camera_subdev_desc *ssdd)
  459. {
  460. if (ssdd->free_bus)
  461. ssdd->free_bus(ssdd);
  462. }
  463. static int mt9m001_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
  464. {
  465. struct i2c_client *client = v4l2_get_subdevdata(sd);
  466. struct mt9m001 *mt9m001 = to_mt9m001(client);
  467. *lines = mt9m001->y_skip_top;
  468. return 0;
  469. }
  470. static const struct v4l2_ctrl_ops mt9m001_ctrl_ops = {
  471. .g_volatile_ctrl = mt9m001_g_volatile_ctrl,
  472. .s_ctrl = mt9m001_s_ctrl,
  473. };
  474. static struct v4l2_subdev_core_ops mt9m001_subdev_core_ops = {
  475. #ifdef CONFIG_VIDEO_ADV_DEBUG
  476. .g_register = mt9m001_g_register,
  477. .s_register = mt9m001_s_register,
  478. #endif
  479. .s_power = mt9m001_s_power,
  480. };
  481. static int mt9m001_enum_mbus_code(struct v4l2_subdev *sd,
  482. struct v4l2_subdev_pad_config *cfg,
  483. struct v4l2_subdev_mbus_code_enum *code)
  484. {
  485. struct i2c_client *client = v4l2_get_subdevdata(sd);
  486. struct mt9m001 *mt9m001 = to_mt9m001(client);
  487. if (code->pad || code->index >= mt9m001->num_fmts)
  488. return -EINVAL;
  489. code->code = mt9m001->fmts[code->index].code;
  490. return 0;
  491. }
  492. static int mt9m001_g_mbus_config(struct v4l2_subdev *sd,
  493. struct v4l2_mbus_config *cfg)
  494. {
  495. struct i2c_client *client = v4l2_get_subdevdata(sd);
  496. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  497. /* MT9M001 has all capture_format parameters fixed */
  498. cfg->flags = V4L2_MBUS_PCLK_SAMPLE_FALLING |
  499. V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH |
  500. V4L2_MBUS_DATA_ACTIVE_HIGH | V4L2_MBUS_MASTER;
  501. cfg->type = V4L2_MBUS_PARALLEL;
  502. cfg->flags = soc_camera_apply_board_flags(ssdd, cfg);
  503. return 0;
  504. }
  505. static int mt9m001_s_mbus_config(struct v4l2_subdev *sd,
  506. const struct v4l2_mbus_config *cfg)
  507. {
  508. const struct i2c_client *client = v4l2_get_subdevdata(sd);
  509. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  510. struct mt9m001 *mt9m001 = to_mt9m001(client);
  511. unsigned int bps = soc_mbus_get_fmtdesc(mt9m001->fmt->code)->bits_per_sample;
  512. if (ssdd->set_bus_param)
  513. return ssdd->set_bus_param(ssdd, 1 << (bps - 1));
  514. /*
  515. * Without board specific bus width settings we only support the
  516. * sensors native bus width
  517. */
  518. return bps == 10 ? 0 : -EINVAL;
  519. }
  520. static struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = {
  521. .s_stream = mt9m001_s_stream,
  522. .s_crop = mt9m001_s_crop,
  523. .g_crop = mt9m001_g_crop,
  524. .cropcap = mt9m001_cropcap,
  525. .g_mbus_config = mt9m001_g_mbus_config,
  526. .s_mbus_config = mt9m001_s_mbus_config,
  527. };
  528. static struct v4l2_subdev_sensor_ops mt9m001_subdev_sensor_ops = {
  529. .g_skip_top_lines = mt9m001_g_skip_top_lines,
  530. };
  531. static const struct v4l2_subdev_pad_ops mt9m001_subdev_pad_ops = {
  532. .enum_mbus_code = mt9m001_enum_mbus_code,
  533. .get_fmt = mt9m001_get_fmt,
  534. .set_fmt = mt9m001_set_fmt,
  535. };
  536. static struct v4l2_subdev_ops mt9m001_subdev_ops = {
  537. .core = &mt9m001_subdev_core_ops,
  538. .video = &mt9m001_subdev_video_ops,
  539. .sensor = &mt9m001_subdev_sensor_ops,
  540. .pad = &mt9m001_subdev_pad_ops,
  541. };
  542. static int mt9m001_probe(struct i2c_client *client,
  543. const struct i2c_device_id *did)
  544. {
  545. struct mt9m001 *mt9m001;
  546. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  547. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  548. int ret;
  549. if (!ssdd) {
  550. dev_err(&client->dev, "MT9M001 driver needs platform data\n");
  551. return -EINVAL;
  552. }
  553. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  554. dev_warn(&adapter->dev,
  555. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  556. return -EIO;
  557. }
  558. mt9m001 = devm_kzalloc(&client->dev, sizeof(struct mt9m001), GFP_KERNEL);
  559. if (!mt9m001)
  560. return -ENOMEM;
  561. v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops);
  562. v4l2_ctrl_handler_init(&mt9m001->hdl, 4);
  563. v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
  564. V4L2_CID_VFLIP, 0, 1, 1, 0);
  565. v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
  566. V4L2_CID_GAIN, 0, 127, 1, 64);
  567. mt9m001->exposure = v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
  568. V4L2_CID_EXPOSURE, 1, 255, 1, 255);
  569. /*
  570. * Simulated autoexposure. If enabled, we calculate shutter width
  571. * ourselves in the driver based on vertical blanking and frame width
  572. */
  573. mt9m001->autoexposure = v4l2_ctrl_new_std_menu(&mt9m001->hdl,
  574. &mt9m001_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
  575. V4L2_EXPOSURE_AUTO);
  576. mt9m001->subdev.ctrl_handler = &mt9m001->hdl;
  577. if (mt9m001->hdl.error)
  578. return mt9m001->hdl.error;
  579. v4l2_ctrl_auto_cluster(2, &mt9m001->autoexposure,
  580. V4L2_EXPOSURE_MANUAL, true);
  581. /* Second stage probe - when a capture adapter is there */
  582. mt9m001->y_skip_top = 0;
  583. mt9m001->rect.left = MT9M001_COLUMN_SKIP;
  584. mt9m001->rect.top = MT9M001_ROW_SKIP;
  585. mt9m001->rect.width = MT9M001_MAX_WIDTH;
  586. mt9m001->rect.height = MT9M001_MAX_HEIGHT;
  587. mt9m001->clk = v4l2_clk_get(&client->dev, "mclk");
  588. if (IS_ERR(mt9m001->clk)) {
  589. ret = PTR_ERR(mt9m001->clk);
  590. goto eclkget;
  591. }
  592. ret = mt9m001_video_probe(ssdd, client);
  593. if (ret) {
  594. v4l2_clk_put(mt9m001->clk);
  595. eclkget:
  596. v4l2_ctrl_handler_free(&mt9m001->hdl);
  597. }
  598. return ret;
  599. }
  600. static int mt9m001_remove(struct i2c_client *client)
  601. {
  602. struct mt9m001 *mt9m001 = to_mt9m001(client);
  603. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  604. v4l2_clk_put(mt9m001->clk);
  605. v4l2_device_unregister_subdev(&mt9m001->subdev);
  606. v4l2_ctrl_handler_free(&mt9m001->hdl);
  607. mt9m001_video_remove(ssdd);
  608. return 0;
  609. }
  610. static const struct i2c_device_id mt9m001_id[] = {
  611. { "mt9m001", 0 },
  612. { }
  613. };
  614. MODULE_DEVICE_TABLE(i2c, mt9m001_id);
  615. static struct i2c_driver mt9m001_i2c_driver = {
  616. .driver = {
  617. .name = "mt9m001",
  618. },
  619. .probe = mt9m001_probe,
  620. .remove = mt9m001_remove,
  621. .id_table = mt9m001_id,
  622. };
  623. module_i2c_driver(mt9m001_i2c_driver);
  624. MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
  625. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  626. MODULE_LICENSE("GPL");