mt9m001.c 19 KB

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