mt9m001.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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_set_bus_param(struct soc_camera_device *icd,
  144. unsigned long flags)
  145. {
  146. struct soc_camera_link *icl = to_soc_camera_link(icd);
  147. unsigned long width_flag = flags & SOCAM_DATAWIDTH_MASK;
  148. /* Only one width bit may be set */
  149. if (!is_power_of_2(width_flag))
  150. return -EINVAL;
  151. if (icl->set_bus_param)
  152. return icl->set_bus_param(icl, width_flag);
  153. /*
  154. * Without board specific bus width settings we only support the
  155. * sensors native bus width
  156. */
  157. if (width_flag == SOCAM_DATAWIDTH_10)
  158. return 0;
  159. return -EINVAL;
  160. }
  161. static unsigned long mt9m001_query_bus_param(struct soc_camera_device *icd)
  162. {
  163. struct soc_camera_link *icl = to_soc_camera_link(icd);
  164. /* MT9M001 has all capture_format parameters fixed */
  165. unsigned long flags = SOCAM_PCLK_SAMPLE_FALLING |
  166. SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |
  167. SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER;
  168. if (icl->query_bus_param)
  169. flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK;
  170. else
  171. flags |= SOCAM_DATAWIDTH_10;
  172. return soc_camera_apply_sensor_flags(icl, flags);
  173. }
  174. static int mt9m001_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  175. {
  176. struct i2c_client *client = v4l2_get_subdevdata(sd);
  177. struct mt9m001 *mt9m001 = to_mt9m001(client);
  178. struct v4l2_rect rect = a->c;
  179. struct soc_camera_device *icd = client->dev.platform_data;
  180. int ret;
  181. const u16 hblank = 9, vblank = 25;
  182. unsigned int total_h;
  183. if (mt9m001->fmts == mt9m001_colour_fmts)
  184. /*
  185. * Bayer format - even number of rows for simplicity,
  186. * but let the user play with the top row.
  187. */
  188. rect.height = ALIGN(rect.height, 2);
  189. /* Datasheet requirement: see register description */
  190. rect.width = ALIGN(rect.width, 2);
  191. rect.left = ALIGN(rect.left, 2);
  192. soc_camera_limit_side(&rect.left, &rect.width,
  193. MT9M001_COLUMN_SKIP, MT9M001_MIN_WIDTH, MT9M001_MAX_WIDTH);
  194. soc_camera_limit_side(&rect.top, &rect.height,
  195. MT9M001_ROW_SKIP, MT9M001_MIN_HEIGHT, MT9M001_MAX_HEIGHT);
  196. total_h = rect.height + mt9m001->y_skip_top + vblank;
  197. /* Blanking and start values - default... */
  198. ret = reg_write(client, MT9M001_HORIZONTAL_BLANKING, hblank);
  199. if (!ret)
  200. ret = reg_write(client, MT9M001_VERTICAL_BLANKING, vblank);
  201. /*
  202. * The caller provides a supported format, as verified per
  203. * call to icd->try_fmt()
  204. */
  205. if (!ret)
  206. ret = reg_write(client, MT9M001_COLUMN_START, rect.left);
  207. if (!ret)
  208. ret = reg_write(client, MT9M001_ROW_START, rect.top);
  209. if (!ret)
  210. ret = reg_write(client, MT9M001_WINDOW_WIDTH, rect.width - 1);
  211. if (!ret)
  212. ret = reg_write(client, MT9M001_WINDOW_HEIGHT,
  213. rect.height + mt9m001->y_skip_top - 1);
  214. if (!ret && mt9m001->autoexposure) {
  215. ret = reg_write(client, MT9M001_SHUTTER_WIDTH, total_h);
  216. if (!ret) {
  217. const struct v4l2_queryctrl *qctrl =
  218. soc_camera_find_qctrl(icd->ops,
  219. V4L2_CID_EXPOSURE);
  220. mt9m001->exposure = (524 + (total_h - 1) *
  221. (qctrl->maximum - qctrl->minimum)) /
  222. 1048 + qctrl->minimum;
  223. }
  224. }
  225. if (!ret)
  226. mt9m001->rect = rect;
  227. return ret;
  228. }
  229. static int mt9m001_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  230. {
  231. struct i2c_client *client = v4l2_get_subdevdata(sd);
  232. struct mt9m001 *mt9m001 = to_mt9m001(client);
  233. a->c = mt9m001->rect;
  234. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  235. return 0;
  236. }
  237. static int mt9m001_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  238. {
  239. a->bounds.left = MT9M001_COLUMN_SKIP;
  240. a->bounds.top = MT9M001_ROW_SKIP;
  241. a->bounds.width = MT9M001_MAX_WIDTH;
  242. a->bounds.height = MT9M001_MAX_HEIGHT;
  243. a->defrect = a->bounds;
  244. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  245. a->pixelaspect.numerator = 1;
  246. a->pixelaspect.denominator = 1;
  247. return 0;
  248. }
  249. static int mt9m001_g_fmt(struct v4l2_subdev *sd,
  250. struct v4l2_mbus_framefmt *mf)
  251. {
  252. struct i2c_client *client = v4l2_get_subdevdata(sd);
  253. struct mt9m001 *mt9m001 = to_mt9m001(client);
  254. mf->width = mt9m001->rect.width;
  255. mf->height = mt9m001->rect.height;
  256. mf->code = mt9m001->fmt->code;
  257. mf->colorspace = mt9m001->fmt->colorspace;
  258. mf->field = V4L2_FIELD_NONE;
  259. return 0;
  260. }
  261. static int mt9m001_s_fmt(struct v4l2_subdev *sd,
  262. struct v4l2_mbus_framefmt *mf)
  263. {
  264. struct i2c_client *client = v4l2_get_subdevdata(sd);
  265. struct mt9m001 *mt9m001 = to_mt9m001(client);
  266. struct v4l2_crop a = {
  267. .c = {
  268. .left = mt9m001->rect.left,
  269. .top = mt9m001->rect.top,
  270. .width = mf->width,
  271. .height = mf->height,
  272. },
  273. };
  274. int ret;
  275. /* No support for scaling so far, just crop. TODO: use skipping */
  276. ret = mt9m001_s_crop(sd, &a);
  277. if (!ret) {
  278. mf->width = mt9m001->rect.width;
  279. mf->height = mt9m001->rect.height;
  280. mt9m001->fmt = mt9m001_find_datafmt(mf->code,
  281. mt9m001->fmts, mt9m001->num_fmts);
  282. mf->colorspace = mt9m001->fmt->colorspace;
  283. }
  284. return ret;
  285. }
  286. static int mt9m001_try_fmt(struct v4l2_subdev *sd,
  287. struct v4l2_mbus_framefmt *mf)
  288. {
  289. struct i2c_client *client = v4l2_get_subdevdata(sd);
  290. struct mt9m001 *mt9m001 = to_mt9m001(client);
  291. const struct mt9m001_datafmt *fmt;
  292. v4l_bound_align_image(&mf->width, MT9M001_MIN_WIDTH,
  293. MT9M001_MAX_WIDTH, 1,
  294. &mf->height, MT9M001_MIN_HEIGHT + mt9m001->y_skip_top,
  295. MT9M001_MAX_HEIGHT + mt9m001->y_skip_top, 0, 0);
  296. if (mt9m001->fmts == mt9m001_colour_fmts)
  297. mf->height = ALIGN(mf->height - 1, 2);
  298. fmt = mt9m001_find_datafmt(mf->code, mt9m001->fmts,
  299. mt9m001->num_fmts);
  300. if (!fmt) {
  301. fmt = mt9m001->fmt;
  302. mf->code = fmt->code;
  303. }
  304. mf->colorspace = fmt->colorspace;
  305. return 0;
  306. }
  307. static int mt9m001_g_chip_ident(struct v4l2_subdev *sd,
  308. struct v4l2_dbg_chip_ident *id)
  309. {
  310. struct i2c_client *client = v4l2_get_subdevdata(sd);
  311. struct mt9m001 *mt9m001 = to_mt9m001(client);
  312. if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  313. return -EINVAL;
  314. if (id->match.addr != client->addr)
  315. return -ENODEV;
  316. id->ident = mt9m001->model;
  317. id->revision = 0;
  318. return 0;
  319. }
  320. #ifdef CONFIG_VIDEO_ADV_DEBUG
  321. static int mt9m001_g_register(struct v4l2_subdev *sd,
  322. struct v4l2_dbg_register *reg)
  323. {
  324. struct i2c_client *client = v4l2_get_subdevdata(sd);
  325. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  326. return -EINVAL;
  327. if (reg->match.addr != client->addr)
  328. return -ENODEV;
  329. reg->size = 2;
  330. reg->val = reg_read(client, reg->reg);
  331. if (reg->val > 0xffff)
  332. return -EIO;
  333. return 0;
  334. }
  335. static int mt9m001_s_register(struct v4l2_subdev *sd,
  336. struct v4l2_dbg_register *reg)
  337. {
  338. struct i2c_client *client = v4l2_get_subdevdata(sd);
  339. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  340. return -EINVAL;
  341. if (reg->match.addr != client->addr)
  342. return -ENODEV;
  343. if (reg_write(client, reg->reg, reg->val) < 0)
  344. return -EIO;
  345. return 0;
  346. }
  347. #endif
  348. static const struct v4l2_queryctrl mt9m001_controls[] = {
  349. {
  350. .id = V4L2_CID_VFLIP,
  351. .type = V4L2_CTRL_TYPE_BOOLEAN,
  352. .name = "Flip Vertically",
  353. .minimum = 0,
  354. .maximum = 1,
  355. .step = 1,
  356. .default_value = 0,
  357. }, {
  358. .id = V4L2_CID_GAIN,
  359. .type = V4L2_CTRL_TYPE_INTEGER,
  360. .name = "Gain",
  361. .minimum = 0,
  362. .maximum = 127,
  363. .step = 1,
  364. .default_value = 64,
  365. .flags = V4L2_CTRL_FLAG_SLIDER,
  366. }, {
  367. .id = V4L2_CID_EXPOSURE,
  368. .type = V4L2_CTRL_TYPE_INTEGER,
  369. .name = "Exposure",
  370. .minimum = 1,
  371. .maximum = 255,
  372. .step = 1,
  373. .default_value = 255,
  374. .flags = V4L2_CTRL_FLAG_SLIDER,
  375. }, {
  376. .id = V4L2_CID_EXPOSURE_AUTO,
  377. .type = V4L2_CTRL_TYPE_BOOLEAN,
  378. .name = "Automatic Exposure",
  379. .minimum = 0,
  380. .maximum = 1,
  381. .step = 1,
  382. .default_value = 1,
  383. }
  384. };
  385. static struct soc_camera_ops mt9m001_ops = {
  386. .set_bus_param = mt9m001_set_bus_param,
  387. .query_bus_param = mt9m001_query_bus_param,
  388. .controls = mt9m001_controls,
  389. .num_controls = ARRAY_SIZE(mt9m001_controls),
  390. };
  391. static int mt9m001_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  392. {
  393. struct i2c_client *client = v4l2_get_subdevdata(sd);
  394. struct mt9m001 *mt9m001 = to_mt9m001(client);
  395. int data;
  396. switch (ctrl->id) {
  397. case V4L2_CID_VFLIP:
  398. data = reg_read(client, MT9M001_READ_OPTIONS2);
  399. if (data < 0)
  400. return -EIO;
  401. ctrl->value = !!(data & 0x8000);
  402. break;
  403. case V4L2_CID_EXPOSURE_AUTO:
  404. ctrl->value = mt9m001->autoexposure;
  405. break;
  406. case V4L2_CID_GAIN:
  407. ctrl->value = mt9m001->gain;
  408. break;
  409. case V4L2_CID_EXPOSURE:
  410. ctrl->value = mt9m001->exposure;
  411. break;
  412. }
  413. return 0;
  414. }
  415. static int mt9m001_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  416. {
  417. struct i2c_client *client = v4l2_get_subdevdata(sd);
  418. struct mt9m001 *mt9m001 = to_mt9m001(client);
  419. struct soc_camera_device *icd = client->dev.platform_data;
  420. const struct v4l2_queryctrl *qctrl;
  421. int data;
  422. qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id);
  423. if (!qctrl)
  424. return -EINVAL;
  425. switch (ctrl->id) {
  426. case V4L2_CID_VFLIP:
  427. if (ctrl->value)
  428. data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000);
  429. else
  430. data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000);
  431. if (data < 0)
  432. return -EIO;
  433. break;
  434. case V4L2_CID_GAIN:
  435. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  436. return -EINVAL;
  437. /* See Datasheet Table 7, Gain settings. */
  438. if (ctrl->value <= qctrl->default_value) {
  439. /* Pack it into 0..1 step 0.125, register values 0..8 */
  440. unsigned long range = qctrl->default_value - qctrl->minimum;
  441. data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
  442. dev_dbg(&client->dev, "Setting gain %d\n", data);
  443. data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
  444. if (data < 0)
  445. return -EIO;
  446. } else {
  447. /* Pack it into 1.125..15 variable step, register values 9..67 */
  448. /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
  449. unsigned long range = qctrl->maximum - qctrl->default_value - 1;
  450. unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
  451. 111 + range / 2) / range + 9;
  452. if (gain <= 32)
  453. data = gain;
  454. else if (gain <= 64)
  455. data = ((gain - 32) * 16 + 16) / 32 + 80;
  456. else
  457. data = ((gain - 64) * 7 + 28) / 56 + 96;
  458. dev_dbg(&client->dev, "Setting gain from %d to %d\n",
  459. reg_read(client, MT9M001_GLOBAL_GAIN), data);
  460. data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
  461. if (data < 0)
  462. return -EIO;
  463. }
  464. /* Success */
  465. mt9m001->gain = ctrl->value;
  466. break;
  467. case V4L2_CID_EXPOSURE:
  468. /* mt9m001 has maximum == default */
  469. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  470. return -EINVAL;
  471. else {
  472. unsigned long range = qctrl->maximum - qctrl->minimum;
  473. unsigned long shutter = ((ctrl->value - qctrl->minimum) * 1048 +
  474. range / 2) / range + 1;
  475. dev_dbg(&client->dev,
  476. "Setting shutter width from %d to %lu\n",
  477. reg_read(client, MT9M001_SHUTTER_WIDTH),
  478. shutter);
  479. if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0)
  480. return -EIO;
  481. mt9m001->exposure = ctrl->value;
  482. mt9m001->autoexposure = 0;
  483. }
  484. break;
  485. case V4L2_CID_EXPOSURE_AUTO:
  486. if (ctrl->value) {
  487. const u16 vblank = 25;
  488. unsigned int total_h = mt9m001->rect.height +
  489. mt9m001->y_skip_top + vblank;
  490. if (reg_write(client, MT9M001_SHUTTER_WIDTH,
  491. total_h) < 0)
  492. return -EIO;
  493. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
  494. mt9m001->exposure = (524 + (total_h - 1) *
  495. (qctrl->maximum - qctrl->minimum)) /
  496. 1048 + qctrl->minimum;
  497. mt9m001->autoexposure = 1;
  498. } else
  499. mt9m001->autoexposure = 0;
  500. break;
  501. }
  502. return 0;
  503. }
  504. /*
  505. * Interface active, can use i2c. If it fails, it can indeed mean, that
  506. * this wasn't our capture interface, so, we wait for the right one
  507. */
  508. static int mt9m001_video_probe(struct soc_camera_device *icd,
  509. struct i2c_client *client)
  510. {
  511. struct mt9m001 *mt9m001 = to_mt9m001(client);
  512. struct soc_camera_link *icl = to_soc_camera_link(icd);
  513. s32 data;
  514. unsigned long flags;
  515. int ret;
  516. /* We must have a parent by now. And it cannot be a wrong one. */
  517. BUG_ON(!icd->parent ||
  518. to_soc_camera_host(icd->parent)->nr != icd->iface);
  519. /* Enable the chip */
  520. data = reg_write(client, MT9M001_CHIP_ENABLE, 1);
  521. dev_dbg(&client->dev, "write: %d\n", data);
  522. /* Read out the chip version register */
  523. data = reg_read(client, MT9M001_CHIP_VERSION);
  524. /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
  525. switch (data) {
  526. case 0x8411:
  527. case 0x8421:
  528. mt9m001->model = V4L2_IDENT_MT9M001C12ST;
  529. mt9m001->fmts = mt9m001_colour_fmts;
  530. break;
  531. case 0x8431:
  532. mt9m001->model = V4L2_IDENT_MT9M001C12STM;
  533. mt9m001->fmts = mt9m001_monochrome_fmts;
  534. break;
  535. default:
  536. dev_err(&client->dev,
  537. "No MT9M001 chip detected, register read %x\n", data);
  538. return -ENODEV;
  539. }
  540. mt9m001->num_fmts = 0;
  541. /*
  542. * This is a 10bit sensor, so by default we only allow 10bit.
  543. * The platform may support different bus widths due to
  544. * different routing of the data lines.
  545. */
  546. if (icl->query_bus_param)
  547. flags = icl->query_bus_param(icl);
  548. else
  549. flags = SOCAM_DATAWIDTH_10;
  550. if (flags & SOCAM_DATAWIDTH_10)
  551. mt9m001->num_fmts++;
  552. else
  553. mt9m001->fmts++;
  554. if (flags & SOCAM_DATAWIDTH_8)
  555. mt9m001->num_fmts++;
  556. mt9m001->fmt = &mt9m001->fmts[0];
  557. dev_info(&client->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
  558. data == 0x8431 ? "C12STM" : "C12ST");
  559. ret = mt9m001_init(client);
  560. if (ret < 0)
  561. dev_err(&client->dev, "Failed to initialise the camera\n");
  562. /* mt9m001_init() has reset the chip, returning registers to defaults */
  563. mt9m001->gain = 64;
  564. mt9m001->exposure = 255;
  565. return ret;
  566. }
  567. static void mt9m001_video_remove(struct soc_camera_device *icd)
  568. {
  569. struct soc_camera_link *icl = to_soc_camera_link(icd);
  570. dev_dbg(icd->pdev, "Video removed: %p, %p\n",
  571. icd->parent, icd->vdev);
  572. if (icl->free_bus)
  573. icl->free_bus(icl);
  574. }
  575. static int mt9m001_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
  576. {
  577. struct i2c_client *client = v4l2_get_subdevdata(sd);
  578. struct mt9m001 *mt9m001 = to_mt9m001(client);
  579. *lines = mt9m001->y_skip_top;
  580. return 0;
  581. }
  582. static struct v4l2_subdev_core_ops mt9m001_subdev_core_ops = {
  583. .g_ctrl = mt9m001_g_ctrl,
  584. .s_ctrl = mt9m001_s_ctrl,
  585. .g_chip_ident = mt9m001_g_chip_ident,
  586. #ifdef CONFIG_VIDEO_ADV_DEBUG
  587. .g_register = mt9m001_g_register,
  588. .s_register = mt9m001_s_register,
  589. #endif
  590. };
  591. static int mt9m001_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  592. enum v4l2_mbus_pixelcode *code)
  593. {
  594. struct i2c_client *client = v4l2_get_subdevdata(sd);
  595. struct mt9m001 *mt9m001 = to_mt9m001(client);
  596. if (index >= mt9m001->num_fmts)
  597. return -EINVAL;
  598. *code = mt9m001->fmts[index].code;
  599. return 0;
  600. }
  601. static int mt9m001_g_mbus_config(struct v4l2_subdev *sd,
  602. struct v4l2_mbus_config *cfg)
  603. {
  604. struct i2c_client *client = v4l2_get_subdevdata(sd);
  605. struct soc_camera_device *icd = client->dev.platform_data;
  606. struct soc_camera_link *icl = to_soc_camera_link(icd);
  607. /* MT9M001 has all capture_format parameters fixed */
  608. cfg->flags = V4L2_MBUS_PCLK_SAMPLE_FALLING |
  609. V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH |
  610. V4L2_MBUS_DATA_ACTIVE_HIGH | V4L2_MBUS_MASTER;
  611. cfg->type = V4L2_MBUS_PARALLEL;
  612. cfg->flags = soc_camera_apply_board_flags(icl, cfg);
  613. return 0;
  614. }
  615. static int mt9m001_s_mbus_config(struct v4l2_subdev *sd,
  616. const struct v4l2_mbus_config *cfg)
  617. {
  618. struct i2c_client *client = v4l2_get_subdevdata(sd);
  619. struct soc_camera_device *icd = client->dev.platform_data;
  620. struct soc_camera_link *icl = to_soc_camera_link(icd);
  621. unsigned int bps = soc_mbus_get_fmtdesc(icd->current_fmt->code)->bits_per_sample;
  622. if (icl->set_bus_param)
  623. return icl->set_bus_param(icl, 1 << (bps - 1));
  624. /*
  625. * Without board specific bus width settings we only support the
  626. * sensors native bus width
  627. */
  628. return bps == 10 ? 0 : -EINVAL;
  629. }
  630. static struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = {
  631. .s_stream = mt9m001_s_stream,
  632. .s_mbus_fmt = mt9m001_s_fmt,
  633. .g_mbus_fmt = mt9m001_g_fmt,
  634. .try_mbus_fmt = mt9m001_try_fmt,
  635. .s_crop = mt9m001_s_crop,
  636. .g_crop = mt9m001_g_crop,
  637. .cropcap = mt9m001_cropcap,
  638. .enum_mbus_fmt = mt9m001_enum_fmt,
  639. .g_mbus_config = mt9m001_g_mbus_config,
  640. .s_mbus_config = mt9m001_s_mbus_config,
  641. };
  642. static struct v4l2_subdev_sensor_ops mt9m001_subdev_sensor_ops = {
  643. .g_skip_top_lines = mt9m001_g_skip_top_lines,
  644. };
  645. static struct v4l2_subdev_ops mt9m001_subdev_ops = {
  646. .core = &mt9m001_subdev_core_ops,
  647. .video = &mt9m001_subdev_video_ops,
  648. .sensor = &mt9m001_subdev_sensor_ops,
  649. };
  650. static int mt9m001_probe(struct i2c_client *client,
  651. const struct i2c_device_id *did)
  652. {
  653. struct mt9m001 *mt9m001;
  654. struct soc_camera_device *icd = client->dev.platform_data;
  655. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  656. struct soc_camera_link *icl;
  657. int ret;
  658. if (!icd) {
  659. dev_err(&client->dev, "MT9M001: missing soc-camera data!\n");
  660. return -EINVAL;
  661. }
  662. icl = to_soc_camera_link(icd);
  663. if (!icl) {
  664. dev_err(&client->dev, "MT9M001 driver needs platform data\n");
  665. return -EINVAL;
  666. }
  667. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  668. dev_warn(&adapter->dev,
  669. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  670. return -EIO;
  671. }
  672. mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL);
  673. if (!mt9m001)
  674. return -ENOMEM;
  675. v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops);
  676. /* Second stage probe - when a capture adapter is there */
  677. icd->ops = &mt9m001_ops;
  678. mt9m001->y_skip_top = 0;
  679. mt9m001->rect.left = MT9M001_COLUMN_SKIP;
  680. mt9m001->rect.top = MT9M001_ROW_SKIP;
  681. mt9m001->rect.width = MT9M001_MAX_WIDTH;
  682. mt9m001->rect.height = MT9M001_MAX_HEIGHT;
  683. /*
  684. * Simulated autoexposure. If enabled, we calculate shutter width
  685. * ourselves in the driver based on vertical blanking and frame width
  686. */
  687. mt9m001->autoexposure = 1;
  688. ret = mt9m001_video_probe(icd, client);
  689. if (ret) {
  690. icd->ops = NULL;
  691. kfree(mt9m001);
  692. }
  693. return ret;
  694. }
  695. static int mt9m001_remove(struct i2c_client *client)
  696. {
  697. struct mt9m001 *mt9m001 = to_mt9m001(client);
  698. struct soc_camera_device *icd = client->dev.platform_data;
  699. icd->ops = NULL;
  700. mt9m001_video_remove(icd);
  701. kfree(mt9m001);
  702. return 0;
  703. }
  704. static const struct i2c_device_id mt9m001_id[] = {
  705. { "mt9m001", 0 },
  706. { }
  707. };
  708. MODULE_DEVICE_TABLE(i2c, mt9m001_id);
  709. static struct i2c_driver mt9m001_i2c_driver = {
  710. .driver = {
  711. .name = "mt9m001",
  712. },
  713. .probe = mt9m001_probe,
  714. .remove = mt9m001_remove,
  715. .id_table = mt9m001_id,
  716. };
  717. static int __init mt9m001_mod_init(void)
  718. {
  719. return i2c_add_driver(&mt9m001_i2c_driver);
  720. }
  721. static void __exit mt9m001_mod_exit(void)
  722. {
  723. i2c_del_driver(&mt9m001_i2c_driver);
  724. }
  725. module_init(mt9m001_mod_init);
  726. module_exit(mt9m001_mod_exit);
  727. MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
  728. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  729. MODULE_LICENSE("GPL");