mt9m001.c 20 KB

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