mt9m001.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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-subdev.h>
  18. #include <media/v4l2-chip-ident.h>
  19. #include <media/v4l2-ctrls.h>
  20. /*
  21. * mt9m001 i2c address 0x5d
  22. * The platform has to define ctruct i2c_board_info objects and link to them
  23. * from struct soc_camera_link
  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. const struct mt9m001_datafmt *fmt;
  87. const struct mt9m001_datafmt *fmts;
  88. int num_fmts;
  89. int model; /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */
  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, 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. static int mt9m001_g_chip_ident(struct v4l2_subdev *sd,
  271. struct v4l2_dbg_chip_ident *id)
  272. {
  273. struct i2c_client *client = v4l2_get_subdevdata(sd);
  274. struct mt9m001 *mt9m001 = to_mt9m001(client);
  275. if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  276. return -EINVAL;
  277. if (id->match.addr != client->addr)
  278. return -ENODEV;
  279. id->ident = mt9m001->model;
  280. id->revision = 0;
  281. return 0;
  282. }
  283. #ifdef CONFIG_VIDEO_ADV_DEBUG
  284. static int mt9m001_g_register(struct v4l2_subdev *sd,
  285. struct v4l2_dbg_register *reg)
  286. {
  287. struct i2c_client *client = v4l2_get_subdevdata(sd);
  288. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  289. return -EINVAL;
  290. if (reg->match.addr != client->addr)
  291. return -ENODEV;
  292. reg->size = 2;
  293. reg->val = reg_read(client, reg->reg);
  294. if (reg->val > 0xffff)
  295. return -EIO;
  296. return 0;
  297. }
  298. static int mt9m001_s_register(struct v4l2_subdev *sd,
  299. struct v4l2_dbg_register *reg)
  300. {
  301. struct i2c_client *client = v4l2_get_subdevdata(sd);
  302. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  303. return -EINVAL;
  304. if (reg->match.addr != client->addr)
  305. return -ENODEV;
  306. if (reg_write(client, reg->reg, reg->val) < 0)
  307. return -EIO;
  308. return 0;
  309. }
  310. #endif
  311. static int mt9m001_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  312. {
  313. struct mt9m001 *mt9m001 = container_of(ctrl->handler,
  314. struct mt9m001, hdl);
  315. s32 min, max;
  316. switch (ctrl->id) {
  317. case V4L2_CID_EXPOSURE_AUTO:
  318. min = mt9m001->exposure->minimum;
  319. max = mt9m001->exposure->maximum;
  320. mt9m001->exposure->val =
  321. (524 + (mt9m001->total_h - 1) * (max - min)) / 1048 + min;
  322. break;
  323. }
  324. return 0;
  325. }
  326. static int mt9m001_s_ctrl(struct v4l2_ctrl *ctrl)
  327. {
  328. struct mt9m001 *mt9m001 = container_of(ctrl->handler,
  329. struct mt9m001, hdl);
  330. struct v4l2_subdev *sd = &mt9m001->subdev;
  331. struct i2c_client *client = v4l2_get_subdevdata(sd);
  332. struct v4l2_ctrl *exp = mt9m001->exposure;
  333. int data;
  334. switch (ctrl->id) {
  335. case V4L2_CID_VFLIP:
  336. if (ctrl->val)
  337. data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000);
  338. else
  339. data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000);
  340. if (data < 0)
  341. return -EIO;
  342. return 0;
  343. case V4L2_CID_GAIN:
  344. /* See Datasheet Table 7, Gain settings. */
  345. if (ctrl->val <= ctrl->default_value) {
  346. /* Pack it into 0..1 step 0.125, register values 0..8 */
  347. unsigned long range = ctrl->default_value - ctrl->minimum;
  348. data = ((ctrl->val - ctrl->minimum) * 8 + range / 2) / range;
  349. dev_dbg(&client->dev, "Setting gain %d\n", data);
  350. data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
  351. if (data < 0)
  352. return -EIO;
  353. } else {
  354. /* Pack it into 1.125..15 variable step, register values 9..67 */
  355. /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
  356. unsigned long range = ctrl->maximum - ctrl->default_value - 1;
  357. unsigned long gain = ((ctrl->val - ctrl->default_value - 1) *
  358. 111 + range / 2) / range + 9;
  359. if (gain <= 32)
  360. data = gain;
  361. else if (gain <= 64)
  362. data = ((gain - 32) * 16 + 16) / 32 + 80;
  363. else
  364. data = ((gain - 64) * 7 + 28) / 56 + 96;
  365. dev_dbg(&client->dev, "Setting gain from %d to %d\n",
  366. reg_read(client, MT9M001_GLOBAL_GAIN), data);
  367. data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
  368. if (data < 0)
  369. return -EIO;
  370. }
  371. return 0;
  372. case V4L2_CID_EXPOSURE_AUTO:
  373. if (ctrl->val == V4L2_EXPOSURE_MANUAL) {
  374. unsigned long range = exp->maximum - exp->minimum;
  375. unsigned long shutter = ((exp->val - exp->minimum) * 1048 +
  376. range / 2) / range + 1;
  377. dev_dbg(&client->dev,
  378. "Setting shutter width from %d to %lu\n",
  379. reg_read(client, MT9M001_SHUTTER_WIDTH), shutter);
  380. if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0)
  381. return -EIO;
  382. } else {
  383. const u16 vblank = 25;
  384. mt9m001->total_h = mt9m001->rect.height +
  385. mt9m001->y_skip_top + vblank;
  386. if (reg_write(client, MT9M001_SHUTTER_WIDTH, mt9m001->total_h) < 0)
  387. return -EIO;
  388. }
  389. return 0;
  390. }
  391. return -EINVAL;
  392. }
  393. /*
  394. * Interface active, can use i2c. If it fails, it can indeed mean, that
  395. * this wasn't our capture interface, so, we wait for the right one
  396. */
  397. static int mt9m001_video_probe(struct soc_camera_link *icl,
  398. struct i2c_client *client)
  399. {
  400. struct mt9m001 *mt9m001 = to_mt9m001(client);
  401. s32 data;
  402. unsigned long flags;
  403. int ret;
  404. /* Enable the chip */
  405. data = reg_write(client, MT9M001_CHIP_ENABLE, 1);
  406. dev_dbg(&client->dev, "write: %d\n", data);
  407. /* Read out the chip version register */
  408. data = reg_read(client, MT9M001_CHIP_VERSION);
  409. /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
  410. switch (data) {
  411. case 0x8411:
  412. case 0x8421:
  413. mt9m001->model = V4L2_IDENT_MT9M001C12ST;
  414. mt9m001->fmts = mt9m001_colour_fmts;
  415. break;
  416. case 0x8431:
  417. mt9m001->model = V4L2_IDENT_MT9M001C12STM;
  418. mt9m001->fmts = mt9m001_monochrome_fmts;
  419. break;
  420. default:
  421. dev_err(&client->dev,
  422. "No MT9M001 chip detected, register read %x\n", data);
  423. return -ENODEV;
  424. }
  425. mt9m001->num_fmts = 0;
  426. /*
  427. * This is a 10bit sensor, so by default we only allow 10bit.
  428. * The platform may support different bus widths due to
  429. * different routing of the data lines.
  430. */
  431. if (icl->query_bus_param)
  432. flags = icl->query_bus_param(icl);
  433. else
  434. flags = SOCAM_DATAWIDTH_10;
  435. if (flags & SOCAM_DATAWIDTH_10)
  436. mt9m001->num_fmts++;
  437. else
  438. mt9m001->fmts++;
  439. if (flags & SOCAM_DATAWIDTH_8)
  440. mt9m001->num_fmts++;
  441. mt9m001->fmt = &mt9m001->fmts[0];
  442. dev_info(&client->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
  443. data == 0x8431 ? "C12STM" : "C12ST");
  444. ret = mt9m001_init(client);
  445. if (ret < 0)
  446. dev_err(&client->dev, "Failed to initialise the camera\n");
  447. /* mt9m001_init() has reset the chip, returning registers to defaults */
  448. return v4l2_ctrl_handler_setup(&mt9m001->hdl);
  449. }
  450. static void mt9m001_video_remove(struct soc_camera_link *icl)
  451. {
  452. if (icl->free_bus)
  453. icl->free_bus(icl);
  454. }
  455. static int mt9m001_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
  456. {
  457. struct i2c_client *client = v4l2_get_subdevdata(sd);
  458. struct mt9m001 *mt9m001 = to_mt9m001(client);
  459. *lines = mt9m001->y_skip_top;
  460. return 0;
  461. }
  462. static const struct v4l2_ctrl_ops mt9m001_ctrl_ops = {
  463. .g_volatile_ctrl = mt9m001_g_volatile_ctrl,
  464. .s_ctrl = mt9m001_s_ctrl,
  465. };
  466. static struct v4l2_subdev_core_ops mt9m001_subdev_core_ops = {
  467. .g_chip_ident = mt9m001_g_chip_ident,
  468. #ifdef CONFIG_VIDEO_ADV_DEBUG
  469. .g_register = mt9m001_g_register,
  470. .s_register = mt9m001_s_register,
  471. #endif
  472. };
  473. static int mt9m001_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  474. enum v4l2_mbus_pixelcode *code)
  475. {
  476. struct i2c_client *client = v4l2_get_subdevdata(sd);
  477. struct mt9m001 *mt9m001 = to_mt9m001(client);
  478. if (index >= mt9m001->num_fmts)
  479. return -EINVAL;
  480. *code = mt9m001->fmts[index].code;
  481. return 0;
  482. }
  483. static int mt9m001_g_mbus_config(struct v4l2_subdev *sd,
  484. struct v4l2_mbus_config *cfg)
  485. {
  486. struct i2c_client *client = v4l2_get_subdevdata(sd);
  487. struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
  488. /* MT9M001 has all capture_format parameters fixed */
  489. cfg->flags = V4L2_MBUS_PCLK_SAMPLE_FALLING |
  490. V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH |
  491. V4L2_MBUS_DATA_ACTIVE_HIGH | V4L2_MBUS_MASTER;
  492. cfg->type = V4L2_MBUS_PARALLEL;
  493. cfg->flags = soc_camera_apply_board_flags(icl, cfg);
  494. return 0;
  495. }
  496. static int mt9m001_s_mbus_config(struct v4l2_subdev *sd,
  497. const struct v4l2_mbus_config *cfg)
  498. {
  499. const struct i2c_client *client = v4l2_get_subdevdata(sd);
  500. struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
  501. struct mt9m001 *mt9m001 = to_mt9m001(client);
  502. unsigned int bps = soc_mbus_get_fmtdesc(mt9m001->fmt->code)->bits_per_sample;
  503. if (icl->set_bus_param)
  504. return icl->set_bus_param(icl, 1 << (bps - 1));
  505. /*
  506. * Without board specific bus width settings we only support the
  507. * sensors native bus width
  508. */
  509. return bps == 10 ? 0 : -EINVAL;
  510. }
  511. static struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = {
  512. .s_stream = mt9m001_s_stream,
  513. .s_mbus_fmt = mt9m001_s_fmt,
  514. .g_mbus_fmt = mt9m001_g_fmt,
  515. .try_mbus_fmt = mt9m001_try_fmt,
  516. .s_crop = mt9m001_s_crop,
  517. .g_crop = mt9m001_g_crop,
  518. .cropcap = mt9m001_cropcap,
  519. .enum_mbus_fmt = mt9m001_enum_fmt,
  520. .g_mbus_config = mt9m001_g_mbus_config,
  521. .s_mbus_config = mt9m001_s_mbus_config,
  522. };
  523. static struct v4l2_subdev_sensor_ops mt9m001_subdev_sensor_ops = {
  524. .g_skip_top_lines = mt9m001_g_skip_top_lines,
  525. };
  526. static struct v4l2_subdev_ops mt9m001_subdev_ops = {
  527. .core = &mt9m001_subdev_core_ops,
  528. .video = &mt9m001_subdev_video_ops,
  529. .sensor = &mt9m001_subdev_sensor_ops,
  530. };
  531. static int mt9m001_probe(struct i2c_client *client,
  532. const struct i2c_device_id *did)
  533. {
  534. struct mt9m001 *mt9m001;
  535. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  536. struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
  537. int ret;
  538. if (!icl) {
  539. dev_err(&client->dev, "MT9M001 driver needs platform data\n");
  540. return -EINVAL;
  541. }
  542. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  543. dev_warn(&adapter->dev,
  544. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  545. return -EIO;
  546. }
  547. mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL);
  548. if (!mt9m001)
  549. return -ENOMEM;
  550. v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops);
  551. v4l2_ctrl_handler_init(&mt9m001->hdl, 4);
  552. v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
  553. V4L2_CID_VFLIP, 0, 1, 1, 0);
  554. v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
  555. V4L2_CID_GAIN, 0, 127, 1, 64);
  556. mt9m001->exposure = v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
  557. V4L2_CID_EXPOSURE, 1, 255, 1, 255);
  558. /*
  559. * Simulated autoexposure. If enabled, we calculate shutter width
  560. * ourselves in the driver based on vertical blanking and frame width
  561. */
  562. mt9m001->autoexposure = v4l2_ctrl_new_std_menu(&mt9m001->hdl,
  563. &mt9m001_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
  564. V4L2_EXPOSURE_AUTO);
  565. mt9m001->subdev.ctrl_handler = &mt9m001->hdl;
  566. if (mt9m001->hdl.error) {
  567. int err = mt9m001->hdl.error;
  568. kfree(mt9m001);
  569. return err;
  570. }
  571. v4l2_ctrl_auto_cluster(2, &mt9m001->autoexposure,
  572. V4L2_EXPOSURE_MANUAL, true);
  573. /* Second stage probe - when a capture adapter is there */
  574. mt9m001->y_skip_top = 0;
  575. mt9m001->rect.left = MT9M001_COLUMN_SKIP;
  576. mt9m001->rect.top = MT9M001_ROW_SKIP;
  577. mt9m001->rect.width = MT9M001_MAX_WIDTH;
  578. mt9m001->rect.height = MT9M001_MAX_HEIGHT;
  579. ret = mt9m001_video_probe(icl, client);
  580. if (ret) {
  581. v4l2_ctrl_handler_free(&mt9m001->hdl);
  582. kfree(mt9m001);
  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_link *icl = soc_camera_i2c_to_link(client);
  590. v4l2_device_unregister_subdev(&mt9m001->subdev);
  591. v4l2_ctrl_handler_free(&mt9m001->hdl);
  592. mt9m001_video_remove(icl);
  593. kfree(mt9m001);
  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");