mt9m001.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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 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. 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, 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. 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_s_power(struct v4l2_subdev *sd, int on)
  312. {
  313. struct i2c_client *client = v4l2_get_subdevdata(sd);
  314. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  315. return soc_camera_set_power(&client->dev, ssdd, on);
  316. }
  317. static int mt9m001_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  318. {
  319. struct mt9m001 *mt9m001 = container_of(ctrl->handler,
  320. struct mt9m001, hdl);
  321. s32 min, max;
  322. switch (ctrl->id) {
  323. case V4L2_CID_EXPOSURE_AUTO:
  324. min = mt9m001->exposure->minimum;
  325. max = mt9m001->exposure->maximum;
  326. mt9m001->exposure->val =
  327. (524 + (mt9m001->total_h - 1) * (max - min)) / 1048 + min;
  328. break;
  329. }
  330. return 0;
  331. }
  332. static int mt9m001_s_ctrl(struct v4l2_ctrl *ctrl)
  333. {
  334. struct mt9m001 *mt9m001 = container_of(ctrl->handler,
  335. struct mt9m001, hdl);
  336. struct v4l2_subdev *sd = &mt9m001->subdev;
  337. struct i2c_client *client = v4l2_get_subdevdata(sd);
  338. struct v4l2_ctrl *exp = mt9m001->exposure;
  339. int data;
  340. switch (ctrl->id) {
  341. case V4L2_CID_VFLIP:
  342. if (ctrl->val)
  343. data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000);
  344. else
  345. data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000);
  346. if (data < 0)
  347. return -EIO;
  348. return 0;
  349. case V4L2_CID_GAIN:
  350. /* See Datasheet Table 7, Gain settings. */
  351. if (ctrl->val <= ctrl->default_value) {
  352. /* Pack it into 0..1 step 0.125, register values 0..8 */
  353. unsigned long range = ctrl->default_value - ctrl->minimum;
  354. data = ((ctrl->val - ctrl->minimum) * 8 + range / 2) / range;
  355. dev_dbg(&client->dev, "Setting gain %d\n", data);
  356. data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
  357. if (data < 0)
  358. return -EIO;
  359. } else {
  360. /* Pack it into 1.125..15 variable step, register values 9..67 */
  361. /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
  362. unsigned long range = ctrl->maximum - ctrl->default_value - 1;
  363. unsigned long gain = ((ctrl->val - ctrl->default_value - 1) *
  364. 111 + range / 2) / range + 9;
  365. if (gain <= 32)
  366. data = gain;
  367. else if (gain <= 64)
  368. data = ((gain - 32) * 16 + 16) / 32 + 80;
  369. else
  370. data = ((gain - 64) * 7 + 28) / 56 + 96;
  371. dev_dbg(&client->dev, "Setting gain from %d to %d\n",
  372. reg_read(client, MT9M001_GLOBAL_GAIN), data);
  373. data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
  374. if (data < 0)
  375. return -EIO;
  376. }
  377. return 0;
  378. case V4L2_CID_EXPOSURE_AUTO:
  379. if (ctrl->val == V4L2_EXPOSURE_MANUAL) {
  380. unsigned long range = exp->maximum - exp->minimum;
  381. unsigned long shutter = ((exp->val - exp->minimum) * 1048 +
  382. range / 2) / range + 1;
  383. dev_dbg(&client->dev,
  384. "Setting shutter width from %d to %lu\n",
  385. reg_read(client, MT9M001_SHUTTER_WIDTH), shutter);
  386. if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0)
  387. return -EIO;
  388. } else {
  389. const u16 vblank = 25;
  390. mt9m001->total_h = mt9m001->rect.height +
  391. mt9m001->y_skip_top + vblank;
  392. if (reg_write(client, MT9M001_SHUTTER_WIDTH, mt9m001->total_h) < 0)
  393. return -EIO;
  394. }
  395. return 0;
  396. }
  397. return -EINVAL;
  398. }
  399. /*
  400. * Interface active, can use i2c. If it fails, it can indeed mean, that
  401. * this wasn't our capture interface, so, we wait for the right one
  402. */
  403. static int mt9m001_video_probe(struct soc_camera_subdev_desc *ssdd,
  404. struct i2c_client *client)
  405. {
  406. struct mt9m001 *mt9m001 = to_mt9m001(client);
  407. s32 data;
  408. unsigned long flags;
  409. int ret;
  410. ret = mt9m001_s_power(&mt9m001->subdev, 1);
  411. if (ret < 0)
  412. return ret;
  413. /* Enable the chip */
  414. data = reg_write(client, MT9M001_CHIP_ENABLE, 1);
  415. dev_dbg(&client->dev, "write: %d\n", data);
  416. /* Read out the chip version register */
  417. data = reg_read(client, MT9M001_CHIP_VERSION);
  418. /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
  419. switch (data) {
  420. case 0x8411:
  421. case 0x8421:
  422. mt9m001->model = V4L2_IDENT_MT9M001C12ST;
  423. mt9m001->fmts = mt9m001_colour_fmts;
  424. break;
  425. case 0x8431:
  426. mt9m001->model = V4L2_IDENT_MT9M001C12STM;
  427. mt9m001->fmts = mt9m001_monochrome_fmts;
  428. break;
  429. default:
  430. dev_err(&client->dev,
  431. "No MT9M001 chip detected, register read %x\n", data);
  432. ret = -ENODEV;
  433. goto done;
  434. }
  435. mt9m001->num_fmts = 0;
  436. /*
  437. * This is a 10bit sensor, so by default we only allow 10bit.
  438. * The platform may support different bus widths due to
  439. * different routing of the data lines.
  440. */
  441. if (ssdd->query_bus_param)
  442. flags = ssdd->query_bus_param(ssdd);
  443. else
  444. flags = SOCAM_DATAWIDTH_10;
  445. if (flags & SOCAM_DATAWIDTH_10)
  446. mt9m001->num_fmts++;
  447. else
  448. mt9m001->fmts++;
  449. if (flags & SOCAM_DATAWIDTH_8)
  450. mt9m001->num_fmts++;
  451. mt9m001->fmt = &mt9m001->fmts[0];
  452. dev_info(&client->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
  453. data == 0x8431 ? "C12STM" : "C12ST");
  454. ret = mt9m001_init(client);
  455. if (ret < 0) {
  456. dev_err(&client->dev, "Failed to initialise the camera\n");
  457. goto done;
  458. }
  459. /* mt9m001_init() has reset the chip, returning registers to defaults */
  460. ret = v4l2_ctrl_handler_setup(&mt9m001->hdl);
  461. done:
  462. mt9m001_s_power(&mt9m001->subdev, 0);
  463. return ret;
  464. }
  465. static void mt9m001_video_remove(struct soc_camera_subdev_desc *ssdd)
  466. {
  467. if (ssdd->free_bus)
  468. ssdd->free_bus(ssdd);
  469. }
  470. static int mt9m001_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
  471. {
  472. struct i2c_client *client = v4l2_get_subdevdata(sd);
  473. struct mt9m001 *mt9m001 = to_mt9m001(client);
  474. *lines = mt9m001->y_skip_top;
  475. return 0;
  476. }
  477. static const struct v4l2_ctrl_ops mt9m001_ctrl_ops = {
  478. .g_volatile_ctrl = mt9m001_g_volatile_ctrl,
  479. .s_ctrl = mt9m001_s_ctrl,
  480. };
  481. static struct v4l2_subdev_core_ops mt9m001_subdev_core_ops = {
  482. .g_chip_ident = mt9m001_g_chip_ident,
  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_fmt(struct v4l2_subdev *sd, unsigned int index,
  490. enum v4l2_mbus_pixelcode *code)
  491. {
  492. struct i2c_client *client = v4l2_get_subdevdata(sd);
  493. struct mt9m001 *mt9m001 = to_mt9m001(client);
  494. if (index >= mt9m001->num_fmts)
  495. return -EINVAL;
  496. *code = mt9m001->fmts[index].code;
  497. return 0;
  498. }
  499. static int mt9m001_g_mbus_config(struct v4l2_subdev *sd,
  500. struct v4l2_mbus_config *cfg)
  501. {
  502. struct i2c_client *client = v4l2_get_subdevdata(sd);
  503. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  504. /* MT9M001 has all capture_format parameters fixed */
  505. cfg->flags = V4L2_MBUS_PCLK_SAMPLE_FALLING |
  506. V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH |
  507. V4L2_MBUS_DATA_ACTIVE_HIGH | V4L2_MBUS_MASTER;
  508. cfg->type = V4L2_MBUS_PARALLEL;
  509. cfg->flags = soc_camera_apply_board_flags(ssdd, cfg);
  510. return 0;
  511. }
  512. static int mt9m001_s_mbus_config(struct v4l2_subdev *sd,
  513. const struct v4l2_mbus_config *cfg)
  514. {
  515. const struct i2c_client *client = v4l2_get_subdevdata(sd);
  516. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  517. struct mt9m001 *mt9m001 = to_mt9m001(client);
  518. unsigned int bps = soc_mbus_get_fmtdesc(mt9m001->fmt->code)->bits_per_sample;
  519. if (ssdd->set_bus_param)
  520. return ssdd->set_bus_param(ssdd, 1 << (bps - 1));
  521. /*
  522. * Without board specific bus width settings we only support the
  523. * sensors native bus width
  524. */
  525. return bps == 10 ? 0 : -EINVAL;
  526. }
  527. static struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = {
  528. .s_stream = mt9m001_s_stream,
  529. .s_mbus_fmt = mt9m001_s_fmt,
  530. .g_mbus_fmt = mt9m001_g_fmt,
  531. .try_mbus_fmt = mt9m001_try_fmt,
  532. .s_crop = mt9m001_s_crop,
  533. .g_crop = mt9m001_g_crop,
  534. .cropcap = mt9m001_cropcap,
  535. .enum_mbus_fmt = mt9m001_enum_fmt,
  536. .g_mbus_config = mt9m001_g_mbus_config,
  537. .s_mbus_config = mt9m001_s_mbus_config,
  538. };
  539. static struct v4l2_subdev_sensor_ops mt9m001_subdev_sensor_ops = {
  540. .g_skip_top_lines = mt9m001_g_skip_top_lines,
  541. };
  542. static struct v4l2_subdev_ops mt9m001_subdev_ops = {
  543. .core = &mt9m001_subdev_core_ops,
  544. .video = &mt9m001_subdev_video_ops,
  545. .sensor = &mt9m001_subdev_sensor_ops,
  546. };
  547. static int mt9m001_probe(struct i2c_client *client,
  548. const struct i2c_device_id *did)
  549. {
  550. struct mt9m001 *mt9m001;
  551. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  552. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  553. int ret;
  554. if (!ssdd) {
  555. dev_err(&client->dev, "MT9M001 driver needs platform data\n");
  556. return -EINVAL;
  557. }
  558. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  559. dev_warn(&adapter->dev,
  560. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  561. return -EIO;
  562. }
  563. mt9m001 = devm_kzalloc(&client->dev, sizeof(struct mt9m001), GFP_KERNEL);
  564. if (!mt9m001)
  565. return -ENOMEM;
  566. v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops);
  567. v4l2_ctrl_handler_init(&mt9m001->hdl, 4);
  568. v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
  569. V4L2_CID_VFLIP, 0, 1, 1, 0);
  570. v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
  571. V4L2_CID_GAIN, 0, 127, 1, 64);
  572. mt9m001->exposure = v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
  573. V4L2_CID_EXPOSURE, 1, 255, 1, 255);
  574. /*
  575. * Simulated autoexposure. If enabled, we calculate shutter width
  576. * ourselves in the driver based on vertical blanking and frame width
  577. */
  578. mt9m001->autoexposure = v4l2_ctrl_new_std_menu(&mt9m001->hdl,
  579. &mt9m001_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
  580. V4L2_EXPOSURE_AUTO);
  581. mt9m001->subdev.ctrl_handler = &mt9m001->hdl;
  582. if (mt9m001->hdl.error)
  583. return mt9m001->hdl.error;
  584. v4l2_ctrl_auto_cluster(2, &mt9m001->autoexposure,
  585. V4L2_EXPOSURE_MANUAL, true);
  586. /* Second stage probe - when a capture adapter is there */
  587. mt9m001->y_skip_top = 0;
  588. mt9m001->rect.left = MT9M001_COLUMN_SKIP;
  589. mt9m001->rect.top = MT9M001_ROW_SKIP;
  590. mt9m001->rect.width = MT9M001_MAX_WIDTH;
  591. mt9m001->rect.height = MT9M001_MAX_HEIGHT;
  592. ret = mt9m001_video_probe(ssdd, client);
  593. if (ret)
  594. v4l2_ctrl_handler_free(&mt9m001->hdl);
  595. return ret;
  596. }
  597. static int mt9m001_remove(struct i2c_client *client)
  598. {
  599. struct mt9m001 *mt9m001 = to_mt9m001(client);
  600. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  601. v4l2_device_unregister_subdev(&mt9m001->subdev);
  602. v4l2_ctrl_handler_free(&mt9m001->hdl);
  603. mt9m001_video_remove(ssdd);
  604. return 0;
  605. }
  606. static const struct i2c_device_id mt9m001_id[] = {
  607. { "mt9m001", 0 },
  608. { }
  609. };
  610. MODULE_DEVICE_TABLE(i2c, mt9m001_id);
  611. static struct i2c_driver mt9m001_i2c_driver = {
  612. .driver = {
  613. .name = "mt9m001",
  614. },
  615. .probe = mt9m001_probe,
  616. .remove = mt9m001_remove,
  617. .id_table = mt9m001_id,
  618. };
  619. module_i2c_driver(mt9m001_i2c_driver);
  620. MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
  621. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  622. MODULE_LICENSE("GPL");