mt9m001.c 20 KB

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