mt9m001.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. enum v4l2_mbus_pixelcode 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. enum v4l2_mbus_pixelcode 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. {V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
  70. {V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
  71. };
  72. static const struct mt9m001_datafmt mt9m001_monochrome_fmts[] = {
  73. /* Order important - see above */
  74. {V4L2_MBUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
  75. {V4L2_MBUS_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 .try_mbus_fmt()
  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_g_fmt(struct v4l2_subdev *sd,
  213. struct v4l2_mbus_framefmt *mf)
  214. {
  215. struct i2c_client *client = v4l2_get_subdevdata(sd);
  216. struct mt9m001 *mt9m001 = to_mt9m001(client);
  217. mf->width = mt9m001->rect.width;
  218. mf->height = mt9m001->rect.height;
  219. mf->code = mt9m001->fmt->code;
  220. mf->colorspace = mt9m001->fmt->colorspace;
  221. mf->field = V4L2_FIELD_NONE;
  222. return 0;
  223. }
  224. static int mt9m001_s_fmt(struct v4l2_subdev *sd,
  225. struct v4l2_mbus_framefmt *mf)
  226. {
  227. struct i2c_client *client = v4l2_get_subdevdata(sd);
  228. struct mt9m001 *mt9m001 = to_mt9m001(client);
  229. struct v4l2_crop a = {
  230. .c = {
  231. .left = mt9m001->rect.left,
  232. .top = mt9m001->rect.top,
  233. .width = mf->width,
  234. .height = mf->height,
  235. },
  236. };
  237. int ret;
  238. /* No support for scaling so far, just crop. TODO: use skipping */
  239. ret = mt9m001_s_crop(sd, &a);
  240. if (!ret) {
  241. mf->width = mt9m001->rect.width;
  242. mf->height = mt9m001->rect.height;
  243. mt9m001->fmt = mt9m001_find_datafmt(mf->code,
  244. mt9m001->fmts, mt9m001->num_fmts);
  245. mf->colorspace = mt9m001->fmt->colorspace;
  246. }
  247. return ret;
  248. }
  249. static int mt9m001_try_fmt(struct v4l2_subdev *sd,
  250. struct v4l2_mbus_framefmt *mf)
  251. {
  252. struct i2c_client *client = v4l2_get_subdevdata(sd);
  253. struct mt9m001 *mt9m001 = to_mt9m001(client);
  254. const struct mt9m001_datafmt *fmt;
  255. v4l_bound_align_image(&mf->width, MT9M001_MIN_WIDTH,
  256. MT9M001_MAX_WIDTH, 1,
  257. &mf->height, MT9M001_MIN_HEIGHT + mt9m001->y_skip_top,
  258. MT9M001_MAX_HEIGHT + mt9m001->y_skip_top, 0, 0);
  259. if (mt9m001->fmts == mt9m001_colour_fmts)
  260. mf->height = ALIGN(mf->height - 1, 2);
  261. fmt = mt9m001_find_datafmt(mf->code, mt9m001->fmts,
  262. mt9m001->num_fmts);
  263. if (!fmt) {
  264. fmt = mt9m001->fmt;
  265. mf->code = fmt->code;
  266. }
  267. mf->colorspace = fmt->colorspace;
  268. return 0;
  269. }
  270. #ifdef CONFIG_VIDEO_ADV_DEBUG
  271. static int mt9m001_g_register(struct v4l2_subdev *sd,
  272. struct v4l2_dbg_register *reg)
  273. {
  274. struct i2c_client *client = v4l2_get_subdevdata(sd);
  275. if (reg->reg > 0xff)
  276. return -EINVAL;
  277. reg->size = 2;
  278. reg->val = reg_read(client, reg->reg);
  279. if (reg->val > 0xffff)
  280. return -EIO;
  281. return 0;
  282. }
  283. static int mt9m001_s_register(struct v4l2_subdev *sd,
  284. const struct v4l2_dbg_register *reg)
  285. {
  286. struct i2c_client *client = v4l2_get_subdevdata(sd);
  287. if (reg->reg > 0xff)
  288. return -EINVAL;
  289. if (reg_write(client, reg->reg, reg->val) < 0)
  290. return -EIO;
  291. return 0;
  292. }
  293. #endif
  294. static int mt9m001_s_power(struct v4l2_subdev *sd, int on)
  295. {
  296. struct i2c_client *client = v4l2_get_subdevdata(sd);
  297. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  298. struct mt9m001 *mt9m001 = to_mt9m001(client);
  299. return soc_camera_set_power(&client->dev, ssdd, mt9m001->clk, on);
  300. }
  301. static int mt9m001_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  302. {
  303. struct mt9m001 *mt9m001 = container_of(ctrl->handler,
  304. struct mt9m001, hdl);
  305. s32 min, max;
  306. switch (ctrl->id) {
  307. case V4L2_CID_EXPOSURE_AUTO:
  308. min = mt9m001->exposure->minimum;
  309. max = mt9m001->exposure->maximum;
  310. mt9m001->exposure->val =
  311. (524 + (mt9m001->total_h - 1) * (max - min)) / 1048 + min;
  312. break;
  313. }
  314. return 0;
  315. }
  316. static int mt9m001_s_ctrl(struct v4l2_ctrl *ctrl)
  317. {
  318. struct mt9m001 *mt9m001 = container_of(ctrl->handler,
  319. struct mt9m001, hdl);
  320. struct v4l2_subdev *sd = &mt9m001->subdev;
  321. struct i2c_client *client = v4l2_get_subdevdata(sd);
  322. struct v4l2_ctrl *exp = mt9m001->exposure;
  323. int data;
  324. switch (ctrl->id) {
  325. case V4L2_CID_VFLIP:
  326. if (ctrl->val)
  327. data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000);
  328. else
  329. data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000);
  330. if (data < 0)
  331. return -EIO;
  332. return 0;
  333. case V4L2_CID_GAIN:
  334. /* See Datasheet Table 7, Gain settings. */
  335. if (ctrl->val <= ctrl->default_value) {
  336. /* Pack it into 0..1 step 0.125, register values 0..8 */
  337. unsigned long range = ctrl->default_value - ctrl->minimum;
  338. data = ((ctrl->val - ctrl->minimum) * 8 + range / 2) / range;
  339. dev_dbg(&client->dev, "Setting gain %d\n", data);
  340. data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
  341. if (data < 0)
  342. return -EIO;
  343. } else {
  344. /* Pack it into 1.125..15 variable step, register values 9..67 */
  345. /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
  346. unsigned long range = ctrl->maximum - ctrl->default_value - 1;
  347. unsigned long gain = ((ctrl->val - ctrl->default_value - 1) *
  348. 111 + range / 2) / range + 9;
  349. if (gain <= 32)
  350. data = gain;
  351. else if (gain <= 64)
  352. data = ((gain - 32) * 16 + 16) / 32 + 80;
  353. else
  354. data = ((gain - 64) * 7 + 28) / 56 + 96;
  355. dev_dbg(&client->dev, "Setting gain from %d to %d\n",
  356. reg_read(client, MT9M001_GLOBAL_GAIN), data);
  357. data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
  358. if (data < 0)
  359. return -EIO;
  360. }
  361. return 0;
  362. case V4L2_CID_EXPOSURE_AUTO:
  363. if (ctrl->val == V4L2_EXPOSURE_MANUAL) {
  364. unsigned long range = exp->maximum - exp->minimum;
  365. unsigned long shutter = ((exp->val - exp->minimum) * 1048 +
  366. range / 2) / range + 1;
  367. dev_dbg(&client->dev,
  368. "Setting shutter width from %d to %lu\n",
  369. reg_read(client, MT9M001_SHUTTER_WIDTH), shutter);
  370. if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0)
  371. return -EIO;
  372. } else {
  373. const u16 vblank = 25;
  374. mt9m001->total_h = mt9m001->rect.height +
  375. mt9m001->y_skip_top + vblank;
  376. if (reg_write(client, MT9M001_SHUTTER_WIDTH, mt9m001->total_h) < 0)
  377. return -EIO;
  378. }
  379. return 0;
  380. }
  381. return -EINVAL;
  382. }
  383. /*
  384. * Interface active, can use i2c. If it fails, it can indeed mean, that
  385. * this wasn't our capture interface, so, we wait for the right one
  386. */
  387. static int mt9m001_video_probe(struct soc_camera_subdev_desc *ssdd,
  388. struct i2c_client *client)
  389. {
  390. struct mt9m001 *mt9m001 = to_mt9m001(client);
  391. s32 data;
  392. unsigned long flags;
  393. int ret;
  394. ret = mt9m001_s_power(&mt9m001->subdev, 1);
  395. if (ret < 0)
  396. return ret;
  397. /* Enable the chip */
  398. data = reg_write(client, MT9M001_CHIP_ENABLE, 1);
  399. dev_dbg(&client->dev, "write: %d\n", data);
  400. /* Read out the chip version register */
  401. data = reg_read(client, MT9M001_CHIP_VERSION);
  402. /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
  403. switch (data) {
  404. case 0x8411:
  405. case 0x8421:
  406. mt9m001->fmts = mt9m001_colour_fmts;
  407. break;
  408. case 0x8431:
  409. mt9m001->fmts = mt9m001_monochrome_fmts;
  410. break;
  411. default:
  412. dev_err(&client->dev,
  413. "No MT9M001 chip detected, register read %x\n", data);
  414. ret = -ENODEV;
  415. goto done;
  416. }
  417. mt9m001->num_fmts = 0;
  418. /*
  419. * This is a 10bit sensor, so by default we only allow 10bit.
  420. * The platform may support different bus widths due to
  421. * different routing of the data lines.
  422. */
  423. if (ssdd->query_bus_param)
  424. flags = ssdd->query_bus_param(ssdd);
  425. else
  426. flags = SOCAM_DATAWIDTH_10;
  427. if (flags & SOCAM_DATAWIDTH_10)
  428. mt9m001->num_fmts++;
  429. else
  430. mt9m001->fmts++;
  431. if (flags & SOCAM_DATAWIDTH_8)
  432. mt9m001->num_fmts++;
  433. mt9m001->fmt = &mt9m001->fmts[0];
  434. dev_info(&client->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
  435. data == 0x8431 ? "C12STM" : "C12ST");
  436. ret = mt9m001_init(client);
  437. if (ret < 0) {
  438. dev_err(&client->dev, "Failed to initialise the camera\n");
  439. goto done;
  440. }
  441. /* mt9m001_init() has reset the chip, returning registers to defaults */
  442. ret = v4l2_ctrl_handler_setup(&mt9m001->hdl);
  443. done:
  444. mt9m001_s_power(&mt9m001->subdev, 0);
  445. return ret;
  446. }
  447. static void mt9m001_video_remove(struct soc_camera_subdev_desc *ssdd)
  448. {
  449. if (ssdd->free_bus)
  450. ssdd->free_bus(ssdd);
  451. }
  452. static int mt9m001_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
  453. {
  454. struct i2c_client *client = v4l2_get_subdevdata(sd);
  455. struct mt9m001 *mt9m001 = to_mt9m001(client);
  456. *lines = mt9m001->y_skip_top;
  457. return 0;
  458. }
  459. static const struct v4l2_ctrl_ops mt9m001_ctrl_ops = {
  460. .g_volatile_ctrl = mt9m001_g_volatile_ctrl,
  461. .s_ctrl = mt9m001_s_ctrl,
  462. };
  463. static struct v4l2_subdev_core_ops mt9m001_subdev_core_ops = {
  464. #ifdef CONFIG_VIDEO_ADV_DEBUG
  465. .g_register = mt9m001_g_register,
  466. .s_register = mt9m001_s_register,
  467. #endif
  468. .s_power = mt9m001_s_power,
  469. };
  470. static int mt9m001_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  471. enum v4l2_mbus_pixelcode *code)
  472. {
  473. struct i2c_client *client = v4l2_get_subdevdata(sd);
  474. struct mt9m001 *mt9m001 = to_mt9m001(client);
  475. if (index >= mt9m001->num_fmts)
  476. return -EINVAL;
  477. *code = mt9m001->fmts[index].code;
  478. return 0;
  479. }
  480. static int mt9m001_g_mbus_config(struct v4l2_subdev *sd,
  481. struct v4l2_mbus_config *cfg)
  482. {
  483. struct i2c_client *client = v4l2_get_subdevdata(sd);
  484. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  485. /* MT9M001 has all capture_format parameters fixed */
  486. cfg->flags = V4L2_MBUS_PCLK_SAMPLE_FALLING |
  487. V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH |
  488. V4L2_MBUS_DATA_ACTIVE_HIGH | V4L2_MBUS_MASTER;
  489. cfg->type = V4L2_MBUS_PARALLEL;
  490. cfg->flags = soc_camera_apply_board_flags(ssdd, cfg);
  491. return 0;
  492. }
  493. static int mt9m001_s_mbus_config(struct v4l2_subdev *sd,
  494. const struct v4l2_mbus_config *cfg)
  495. {
  496. const struct i2c_client *client = v4l2_get_subdevdata(sd);
  497. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  498. struct mt9m001 *mt9m001 = to_mt9m001(client);
  499. unsigned int bps = soc_mbus_get_fmtdesc(mt9m001->fmt->code)->bits_per_sample;
  500. if (ssdd->set_bus_param)
  501. return ssdd->set_bus_param(ssdd, 1 << (bps - 1));
  502. /*
  503. * Without board specific bus width settings we only support the
  504. * sensors native bus width
  505. */
  506. return bps == 10 ? 0 : -EINVAL;
  507. }
  508. static struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = {
  509. .s_stream = mt9m001_s_stream,
  510. .s_mbus_fmt = mt9m001_s_fmt,
  511. .g_mbus_fmt = mt9m001_g_fmt,
  512. .try_mbus_fmt = mt9m001_try_fmt,
  513. .s_crop = mt9m001_s_crop,
  514. .g_crop = mt9m001_g_crop,
  515. .cropcap = mt9m001_cropcap,
  516. .enum_mbus_fmt = mt9m001_enum_fmt,
  517. .g_mbus_config = mt9m001_g_mbus_config,
  518. .s_mbus_config = mt9m001_s_mbus_config,
  519. };
  520. static struct v4l2_subdev_sensor_ops mt9m001_subdev_sensor_ops = {
  521. .g_skip_top_lines = mt9m001_g_skip_top_lines,
  522. };
  523. static struct v4l2_subdev_ops mt9m001_subdev_ops = {
  524. .core = &mt9m001_subdev_core_ops,
  525. .video = &mt9m001_subdev_video_ops,
  526. .sensor = &mt9m001_subdev_sensor_ops,
  527. };
  528. static int mt9m001_probe(struct i2c_client *client,
  529. const struct i2c_device_id *did)
  530. {
  531. struct mt9m001 *mt9m001;
  532. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  533. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  534. int ret;
  535. if (!ssdd) {
  536. dev_err(&client->dev, "MT9M001 driver needs platform data\n");
  537. return -EINVAL;
  538. }
  539. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  540. dev_warn(&adapter->dev,
  541. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  542. return -EIO;
  543. }
  544. mt9m001 = devm_kzalloc(&client->dev, sizeof(struct mt9m001), GFP_KERNEL);
  545. if (!mt9m001)
  546. return -ENOMEM;
  547. v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops);
  548. v4l2_ctrl_handler_init(&mt9m001->hdl, 4);
  549. v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
  550. V4L2_CID_VFLIP, 0, 1, 1, 0);
  551. v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
  552. V4L2_CID_GAIN, 0, 127, 1, 64);
  553. mt9m001->exposure = v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
  554. V4L2_CID_EXPOSURE, 1, 255, 1, 255);
  555. /*
  556. * Simulated autoexposure. If enabled, we calculate shutter width
  557. * ourselves in the driver based on vertical blanking and frame width
  558. */
  559. mt9m001->autoexposure = v4l2_ctrl_new_std_menu(&mt9m001->hdl,
  560. &mt9m001_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
  561. V4L2_EXPOSURE_AUTO);
  562. mt9m001->subdev.ctrl_handler = &mt9m001->hdl;
  563. if (mt9m001->hdl.error)
  564. return mt9m001->hdl.error;
  565. v4l2_ctrl_auto_cluster(2, &mt9m001->autoexposure,
  566. V4L2_EXPOSURE_MANUAL, true);
  567. /* Second stage probe - when a capture adapter is there */
  568. mt9m001->y_skip_top = 0;
  569. mt9m001->rect.left = MT9M001_COLUMN_SKIP;
  570. mt9m001->rect.top = MT9M001_ROW_SKIP;
  571. mt9m001->rect.width = MT9M001_MAX_WIDTH;
  572. mt9m001->rect.height = MT9M001_MAX_HEIGHT;
  573. mt9m001->clk = v4l2_clk_get(&client->dev, "mclk");
  574. if (IS_ERR(mt9m001->clk)) {
  575. ret = PTR_ERR(mt9m001->clk);
  576. goto eclkget;
  577. }
  578. ret = mt9m001_video_probe(ssdd, client);
  579. if (ret) {
  580. v4l2_clk_put(mt9m001->clk);
  581. eclkget:
  582. v4l2_ctrl_handler_free(&mt9m001->hdl);
  583. }
  584. return ret;
  585. }
  586. static int mt9m001_remove(struct i2c_client *client)
  587. {
  588. struct mt9m001 *mt9m001 = to_mt9m001(client);
  589. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  590. v4l2_clk_put(mt9m001->clk);
  591. v4l2_device_unregister_subdev(&mt9m001->subdev);
  592. v4l2_ctrl_handler_free(&mt9m001->hdl);
  593. mt9m001_video_remove(ssdd);
  594. return 0;
  595. }
  596. static const struct i2c_device_id mt9m001_id[] = {
  597. { "mt9m001", 0 },
  598. { }
  599. };
  600. MODULE_DEVICE_TABLE(i2c, mt9m001_id);
  601. static struct i2c_driver mt9m001_i2c_driver = {
  602. .driver = {
  603. .name = "mt9m001",
  604. },
  605. .probe = mt9m001_probe,
  606. .remove = mt9m001_remove,
  607. .id_table = mt9m001_id,
  608. };
  609. module_i2c_driver(mt9m001_i2c_driver);
  610. MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
  611. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  612. MODULE_LICENSE("GPL");