mt9m001.c 20 KB

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