mt9m001.c 21 KB

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