mt9m001.c 21 KB

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