mt9m001.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for MT9M001 CMOS Image Sensor from Micron
  4. *
  5. * Copyright (C) 2008, Guennadi Liakhovetski <[email protected]>
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/i2c.h>
  11. #include <linux/log2.h>
  12. #include <linux/module.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/slab.h>
  15. #include <linux/videodev2.h>
  16. #include <media/v4l2-ctrls.h>
  17. #include <media/v4l2-device.h>
  18. #include <media/v4l2-event.h>
  19. #include <media/v4l2-subdev.h>
  20. /*
  21. * mt9m001 i2c address 0x5d
  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. #define MT9M001_DEFAULT_HBLANK 9
  47. #define MT9M001_DEFAULT_VBLANK 25
  48. /* MT9M001 has only one fixed colorspace per pixelcode */
  49. struct mt9m001_datafmt {
  50. u32 code;
  51. enum v4l2_colorspace colorspace;
  52. };
  53. /* Find a data format by a pixel code in an array */
  54. static const struct mt9m001_datafmt *mt9m001_find_datafmt(
  55. u32 code, const struct mt9m001_datafmt *fmt,
  56. int n)
  57. {
  58. int i;
  59. for (i = 0; i < n; i++)
  60. if (fmt[i].code == code)
  61. return fmt + i;
  62. return NULL;
  63. }
  64. static const struct mt9m001_datafmt mt9m001_colour_fmts[] = {
  65. /*
  66. * Order important: first natively supported,
  67. * second supported with a GPIO extender
  68. */
  69. {MEDIA_BUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
  70. {MEDIA_BUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
  71. };
  72. static const struct mt9m001_datafmt mt9m001_monochrome_fmts[] = {
  73. /* Order important - see above */
  74. {MEDIA_BUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
  75. {MEDIA_BUS_FMT_Y8_1X8, V4L2_COLORSPACE_JPEG},
  76. };
  77. struct mt9m001 {
  78. struct v4l2_subdev subdev;
  79. struct v4l2_ctrl_handler hdl;
  80. struct {
  81. /* exposure/auto-exposure cluster */
  82. struct v4l2_ctrl *autoexposure;
  83. struct v4l2_ctrl *exposure;
  84. };
  85. bool streaming;
  86. struct mutex mutex;
  87. struct v4l2_rect rect; /* Sensor window */
  88. struct clk *clk;
  89. struct gpio_desc *standby_gpio;
  90. struct gpio_desc *reset_gpio;
  91. const struct mt9m001_datafmt *fmt;
  92. const struct mt9m001_datafmt *fmts;
  93. int num_fmts;
  94. unsigned int total_h;
  95. unsigned short y_skip_top; /* Lines to skip at the top */
  96. struct media_pad pad;
  97. };
  98. static struct mt9m001 *to_mt9m001(const struct i2c_client *client)
  99. {
  100. return container_of(i2c_get_clientdata(client), struct mt9m001, subdev);
  101. }
  102. static int reg_read(struct i2c_client *client, const u8 reg)
  103. {
  104. return i2c_smbus_read_word_swapped(client, reg);
  105. }
  106. static int reg_write(struct i2c_client *client, const u8 reg,
  107. const u16 data)
  108. {
  109. return i2c_smbus_write_word_swapped(client, reg, data);
  110. }
  111. static int reg_set(struct i2c_client *client, const u8 reg,
  112. const u16 data)
  113. {
  114. int ret;
  115. ret = reg_read(client, reg);
  116. if (ret < 0)
  117. return ret;
  118. return reg_write(client, reg, ret | data);
  119. }
  120. static int reg_clear(struct i2c_client *client, const u8 reg,
  121. const u16 data)
  122. {
  123. int ret;
  124. ret = reg_read(client, reg);
  125. if (ret < 0)
  126. return ret;
  127. return reg_write(client, reg, ret & ~data);
  128. }
  129. struct mt9m001_reg {
  130. u8 reg;
  131. u16 data;
  132. };
  133. static int multi_reg_write(struct i2c_client *client,
  134. const struct mt9m001_reg *regs, int num)
  135. {
  136. int i;
  137. for (i = 0; i < num; i++) {
  138. int ret = reg_write(client, regs[i].reg, regs[i].data);
  139. if (ret)
  140. return ret;
  141. }
  142. return 0;
  143. }
  144. static int mt9m001_init(struct i2c_client *client)
  145. {
  146. static const struct mt9m001_reg init_regs[] = {
  147. /*
  148. * Issue a soft reset. This returns all registers to their
  149. * default values.
  150. */
  151. { MT9M001_RESET, 1 },
  152. { MT9M001_RESET, 0 },
  153. /* Disable chip, synchronous option update */
  154. { MT9M001_OUTPUT_CONTROL, 0 }
  155. };
  156. dev_dbg(&client->dev, "%s\n", __func__);
  157. return multi_reg_write(client, init_regs, ARRAY_SIZE(init_regs));
  158. }
  159. static int mt9m001_apply_selection(struct v4l2_subdev *sd)
  160. {
  161. struct i2c_client *client = v4l2_get_subdevdata(sd);
  162. struct mt9m001 *mt9m001 = to_mt9m001(client);
  163. const struct mt9m001_reg regs[] = {
  164. /* Blanking and start values - default... */
  165. { MT9M001_HORIZONTAL_BLANKING, MT9M001_DEFAULT_HBLANK },
  166. { MT9M001_VERTICAL_BLANKING, MT9M001_DEFAULT_VBLANK },
  167. /*
  168. * The caller provides a supported format, as verified per
  169. * call to .set_fmt(FORMAT_TRY).
  170. */
  171. { MT9M001_COLUMN_START, mt9m001->rect.left },
  172. { MT9M001_ROW_START, mt9m001->rect.top },
  173. { MT9M001_WINDOW_WIDTH, mt9m001->rect.width - 1 },
  174. { MT9M001_WINDOW_HEIGHT,
  175. mt9m001->rect.height + mt9m001->y_skip_top - 1 },
  176. };
  177. return multi_reg_write(client, regs, ARRAY_SIZE(regs));
  178. }
  179. static int mt9m001_s_stream(struct v4l2_subdev *sd, int enable)
  180. {
  181. struct i2c_client *client = v4l2_get_subdevdata(sd);
  182. struct mt9m001 *mt9m001 = to_mt9m001(client);
  183. int ret = 0;
  184. mutex_lock(&mt9m001->mutex);
  185. if (mt9m001->streaming == enable)
  186. goto done;
  187. if (enable) {
  188. ret = pm_runtime_resume_and_get(&client->dev);
  189. if (ret < 0)
  190. goto unlock;
  191. ret = mt9m001_apply_selection(sd);
  192. if (ret)
  193. goto put_unlock;
  194. ret = __v4l2_ctrl_handler_setup(&mt9m001->hdl);
  195. if (ret)
  196. goto put_unlock;
  197. /* Switch to master "normal" mode */
  198. ret = reg_write(client, MT9M001_OUTPUT_CONTROL, 2);
  199. if (ret < 0)
  200. goto put_unlock;
  201. } else {
  202. /* Switch to master stop sensor readout */
  203. reg_write(client, MT9M001_OUTPUT_CONTROL, 0);
  204. pm_runtime_put(&client->dev);
  205. }
  206. mt9m001->streaming = enable;
  207. done:
  208. mutex_unlock(&mt9m001->mutex);
  209. return 0;
  210. put_unlock:
  211. pm_runtime_put(&client->dev);
  212. unlock:
  213. mutex_unlock(&mt9m001->mutex);
  214. return ret;
  215. }
  216. static int mt9m001_set_selection(struct v4l2_subdev *sd,
  217. struct v4l2_subdev_state *sd_state,
  218. struct v4l2_subdev_selection *sel)
  219. {
  220. struct i2c_client *client = v4l2_get_subdevdata(sd);
  221. struct mt9m001 *mt9m001 = to_mt9m001(client);
  222. struct v4l2_rect rect = sel->r;
  223. if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE ||
  224. sel->target != V4L2_SEL_TGT_CROP)
  225. return -EINVAL;
  226. if (mt9m001->fmts == mt9m001_colour_fmts)
  227. /*
  228. * Bayer format - even number of rows for simplicity,
  229. * but let the user play with the top row.
  230. */
  231. rect.height = ALIGN(rect.height, 2);
  232. /* Datasheet requirement: see register description */
  233. rect.width = ALIGN(rect.width, 2);
  234. rect.left = ALIGN(rect.left, 2);
  235. rect.width = clamp_t(u32, rect.width, MT9M001_MIN_WIDTH,
  236. MT9M001_MAX_WIDTH);
  237. rect.left = clamp_t(u32, rect.left, MT9M001_COLUMN_SKIP,
  238. MT9M001_COLUMN_SKIP + MT9M001_MAX_WIDTH - rect.width);
  239. rect.height = clamp_t(u32, rect.height, MT9M001_MIN_HEIGHT,
  240. MT9M001_MAX_HEIGHT);
  241. rect.top = clamp_t(u32, rect.top, MT9M001_ROW_SKIP,
  242. MT9M001_ROW_SKIP + MT9M001_MAX_HEIGHT - rect.height);
  243. mt9m001->total_h = rect.height + mt9m001->y_skip_top +
  244. MT9M001_DEFAULT_VBLANK;
  245. mt9m001->rect = rect;
  246. return 0;
  247. }
  248. static int mt9m001_get_selection(struct v4l2_subdev *sd,
  249. struct v4l2_subdev_state *sd_state,
  250. struct v4l2_subdev_selection *sel)
  251. {
  252. struct i2c_client *client = v4l2_get_subdevdata(sd);
  253. struct mt9m001 *mt9m001 = to_mt9m001(client);
  254. if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  255. return -EINVAL;
  256. switch (sel->target) {
  257. case V4L2_SEL_TGT_CROP_BOUNDS:
  258. sel->r.left = MT9M001_COLUMN_SKIP;
  259. sel->r.top = MT9M001_ROW_SKIP;
  260. sel->r.width = MT9M001_MAX_WIDTH;
  261. sel->r.height = MT9M001_MAX_HEIGHT;
  262. return 0;
  263. case V4L2_SEL_TGT_CROP:
  264. sel->r = mt9m001->rect;
  265. return 0;
  266. default:
  267. return -EINVAL;
  268. }
  269. }
  270. static int mt9m001_get_fmt(struct v4l2_subdev *sd,
  271. struct v4l2_subdev_state *sd_state,
  272. struct v4l2_subdev_format *format)
  273. {
  274. struct i2c_client *client = v4l2_get_subdevdata(sd);
  275. struct mt9m001 *mt9m001 = to_mt9m001(client);
  276. struct v4l2_mbus_framefmt *mf = &format->format;
  277. if (format->pad)
  278. return -EINVAL;
  279. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  280. mf = v4l2_subdev_get_try_format(sd, sd_state, 0);
  281. format->format = *mf;
  282. return 0;
  283. }
  284. mf->width = mt9m001->rect.width;
  285. mf->height = mt9m001->rect.height;
  286. mf->code = mt9m001->fmt->code;
  287. mf->colorspace = mt9m001->fmt->colorspace;
  288. mf->field = V4L2_FIELD_NONE;
  289. mf->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  290. mf->quantization = V4L2_QUANTIZATION_DEFAULT;
  291. mf->xfer_func = V4L2_XFER_FUNC_DEFAULT;
  292. return 0;
  293. }
  294. static int mt9m001_s_fmt(struct v4l2_subdev *sd,
  295. const struct mt9m001_datafmt *fmt,
  296. struct v4l2_mbus_framefmt *mf)
  297. {
  298. struct i2c_client *client = v4l2_get_subdevdata(sd);
  299. struct mt9m001 *mt9m001 = to_mt9m001(client);
  300. struct v4l2_subdev_selection sel = {
  301. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  302. .target = V4L2_SEL_TGT_CROP,
  303. .r.left = mt9m001->rect.left,
  304. .r.top = mt9m001->rect.top,
  305. .r.width = mf->width,
  306. .r.height = mf->height,
  307. };
  308. int ret;
  309. /* No support for scaling so far, just crop. TODO: use skipping */
  310. ret = mt9m001_set_selection(sd, NULL, &sel);
  311. if (!ret) {
  312. mf->width = mt9m001->rect.width;
  313. mf->height = mt9m001->rect.height;
  314. mt9m001->fmt = fmt;
  315. mf->colorspace = fmt->colorspace;
  316. }
  317. return ret;
  318. }
  319. static int mt9m001_set_fmt(struct v4l2_subdev *sd,
  320. struct v4l2_subdev_state *sd_state,
  321. struct v4l2_subdev_format *format)
  322. {
  323. struct v4l2_mbus_framefmt *mf = &format->format;
  324. struct i2c_client *client = v4l2_get_subdevdata(sd);
  325. struct mt9m001 *mt9m001 = to_mt9m001(client);
  326. const struct mt9m001_datafmt *fmt;
  327. if (format->pad)
  328. return -EINVAL;
  329. v4l_bound_align_image(&mf->width, MT9M001_MIN_WIDTH,
  330. MT9M001_MAX_WIDTH, 1,
  331. &mf->height, MT9M001_MIN_HEIGHT + mt9m001->y_skip_top,
  332. MT9M001_MAX_HEIGHT + mt9m001->y_skip_top, 0, 0);
  333. if (mt9m001->fmts == mt9m001_colour_fmts)
  334. mf->height = ALIGN(mf->height - 1, 2);
  335. fmt = mt9m001_find_datafmt(mf->code, mt9m001->fmts,
  336. mt9m001->num_fmts);
  337. if (!fmt) {
  338. fmt = mt9m001->fmt;
  339. mf->code = fmt->code;
  340. }
  341. mf->colorspace = fmt->colorspace;
  342. mf->field = V4L2_FIELD_NONE;
  343. mf->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  344. mf->quantization = V4L2_QUANTIZATION_DEFAULT;
  345. mf->xfer_func = V4L2_XFER_FUNC_DEFAULT;
  346. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  347. return mt9m001_s_fmt(sd, fmt, mf);
  348. sd_state->pads->try_fmt = *mf;
  349. return 0;
  350. }
  351. #ifdef CONFIG_VIDEO_ADV_DEBUG
  352. static int mt9m001_g_register(struct v4l2_subdev *sd,
  353. struct v4l2_dbg_register *reg)
  354. {
  355. struct i2c_client *client = v4l2_get_subdevdata(sd);
  356. if (reg->reg > 0xff)
  357. return -EINVAL;
  358. reg->size = 2;
  359. reg->val = reg_read(client, reg->reg);
  360. if (reg->val > 0xffff)
  361. return -EIO;
  362. return 0;
  363. }
  364. static int mt9m001_s_register(struct v4l2_subdev *sd,
  365. const struct v4l2_dbg_register *reg)
  366. {
  367. struct i2c_client *client = v4l2_get_subdevdata(sd);
  368. if (reg->reg > 0xff)
  369. return -EINVAL;
  370. if (reg_write(client, reg->reg, reg->val) < 0)
  371. return -EIO;
  372. return 0;
  373. }
  374. #endif
  375. static int mt9m001_power_on(struct device *dev)
  376. {
  377. struct i2c_client *client = to_i2c_client(dev);
  378. struct mt9m001 *mt9m001 = to_mt9m001(client);
  379. int ret;
  380. ret = clk_prepare_enable(mt9m001->clk);
  381. if (ret)
  382. return ret;
  383. if (mt9m001->standby_gpio) {
  384. gpiod_set_value_cansleep(mt9m001->standby_gpio, 0);
  385. usleep_range(1000, 2000);
  386. }
  387. if (mt9m001->reset_gpio) {
  388. gpiod_set_value_cansleep(mt9m001->reset_gpio, 1);
  389. usleep_range(1000, 2000);
  390. gpiod_set_value_cansleep(mt9m001->reset_gpio, 0);
  391. usleep_range(1000, 2000);
  392. }
  393. return 0;
  394. }
  395. static int mt9m001_power_off(struct device *dev)
  396. {
  397. struct i2c_client *client = to_i2c_client(dev);
  398. struct mt9m001 *mt9m001 = to_mt9m001(client);
  399. gpiod_set_value_cansleep(mt9m001->standby_gpio, 1);
  400. clk_disable_unprepare(mt9m001->clk);
  401. return 0;
  402. }
  403. static int mt9m001_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  404. {
  405. struct mt9m001 *mt9m001 = container_of(ctrl->handler,
  406. struct mt9m001, hdl);
  407. s32 min, max;
  408. switch (ctrl->id) {
  409. case V4L2_CID_EXPOSURE_AUTO:
  410. min = mt9m001->exposure->minimum;
  411. max = mt9m001->exposure->maximum;
  412. mt9m001->exposure->val =
  413. (524 + (mt9m001->total_h - 1) * (max - min)) / 1048 + min;
  414. break;
  415. }
  416. return 0;
  417. }
  418. static int mt9m001_s_ctrl(struct v4l2_ctrl *ctrl)
  419. {
  420. struct mt9m001 *mt9m001 = container_of(ctrl->handler,
  421. struct mt9m001, hdl);
  422. struct v4l2_subdev *sd = &mt9m001->subdev;
  423. struct i2c_client *client = v4l2_get_subdevdata(sd);
  424. struct v4l2_ctrl *exp = mt9m001->exposure;
  425. int data;
  426. int ret;
  427. if (!pm_runtime_get_if_in_use(&client->dev))
  428. return 0;
  429. switch (ctrl->id) {
  430. case V4L2_CID_VFLIP:
  431. if (ctrl->val)
  432. ret = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000);
  433. else
  434. ret = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000);
  435. break;
  436. case V4L2_CID_GAIN:
  437. /* See Datasheet Table 7, Gain settings. */
  438. if (ctrl->val <= ctrl->default_value) {
  439. /* Pack it into 0..1 step 0.125, register values 0..8 */
  440. unsigned long range = ctrl->default_value - ctrl->minimum;
  441. data = ((ctrl->val - (s32)ctrl->minimum) * 8 + range / 2) / range;
  442. dev_dbg(&client->dev, "Setting gain %d\n", data);
  443. ret = reg_write(client, MT9M001_GLOBAL_GAIN, data);
  444. } else {
  445. /* Pack it into 1.125..15 variable step, register values 9..67 */
  446. /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
  447. unsigned long range = ctrl->maximum - ctrl->default_value - 1;
  448. unsigned long gain = ((ctrl->val - (s32)ctrl->default_value - 1) *
  449. 111 + range / 2) / range + 9;
  450. if (gain <= 32)
  451. data = gain;
  452. else if (gain <= 64)
  453. data = ((gain - 32) * 16 + 16) / 32 + 80;
  454. else
  455. data = ((gain - 64) * 7 + 28) / 56 + 96;
  456. dev_dbg(&client->dev, "Setting gain from %d to %d\n",
  457. reg_read(client, MT9M001_GLOBAL_GAIN), data);
  458. ret = reg_write(client, MT9M001_GLOBAL_GAIN, data);
  459. }
  460. break;
  461. case V4L2_CID_EXPOSURE_AUTO:
  462. if (ctrl->val == V4L2_EXPOSURE_MANUAL) {
  463. unsigned long range = exp->maximum - exp->minimum;
  464. unsigned long shutter = ((exp->val - (s32)exp->minimum) * 1048 +
  465. range / 2) / range + 1;
  466. dev_dbg(&client->dev,
  467. "Setting shutter width from %d to %lu\n",
  468. reg_read(client, MT9M001_SHUTTER_WIDTH), shutter);
  469. ret = reg_write(client, MT9M001_SHUTTER_WIDTH, shutter);
  470. } else {
  471. mt9m001->total_h = mt9m001->rect.height +
  472. mt9m001->y_skip_top + MT9M001_DEFAULT_VBLANK;
  473. ret = reg_write(client, MT9M001_SHUTTER_WIDTH,
  474. mt9m001->total_h);
  475. }
  476. break;
  477. default:
  478. ret = -EINVAL;
  479. break;
  480. }
  481. pm_runtime_put(&client->dev);
  482. return ret;
  483. }
  484. /*
  485. * Interface active, can use i2c. If it fails, it can indeed mean, that
  486. * this wasn't our capture interface, so, we wait for the right one
  487. */
  488. static int mt9m001_video_probe(struct i2c_client *client)
  489. {
  490. struct mt9m001 *mt9m001 = to_mt9m001(client);
  491. s32 data;
  492. int ret;
  493. /* Enable the chip */
  494. data = reg_write(client, MT9M001_CHIP_ENABLE, 1);
  495. dev_dbg(&client->dev, "write: %d\n", data);
  496. /* Read out the chip version register */
  497. data = reg_read(client, MT9M001_CHIP_VERSION);
  498. /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
  499. switch (data) {
  500. case 0x8411:
  501. case 0x8421:
  502. mt9m001->fmts = mt9m001_colour_fmts;
  503. mt9m001->num_fmts = ARRAY_SIZE(mt9m001_colour_fmts);
  504. break;
  505. case 0x8431:
  506. mt9m001->fmts = mt9m001_monochrome_fmts;
  507. mt9m001->num_fmts = ARRAY_SIZE(mt9m001_monochrome_fmts);
  508. break;
  509. default:
  510. dev_err(&client->dev,
  511. "No MT9M001 chip detected, register read %x\n", data);
  512. ret = -ENODEV;
  513. goto done;
  514. }
  515. mt9m001->fmt = &mt9m001->fmts[0];
  516. dev_info(&client->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
  517. data == 0x8431 ? "C12STM" : "C12ST");
  518. ret = mt9m001_init(client);
  519. if (ret < 0) {
  520. dev_err(&client->dev, "Failed to initialise the camera\n");
  521. goto done;
  522. }
  523. /* mt9m001_init() has reset the chip, returning registers to defaults */
  524. ret = v4l2_ctrl_handler_setup(&mt9m001->hdl);
  525. done:
  526. return ret;
  527. }
  528. static int mt9m001_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
  529. {
  530. struct i2c_client *client = v4l2_get_subdevdata(sd);
  531. struct mt9m001 *mt9m001 = to_mt9m001(client);
  532. *lines = mt9m001->y_skip_top;
  533. return 0;
  534. }
  535. static const struct v4l2_ctrl_ops mt9m001_ctrl_ops = {
  536. .g_volatile_ctrl = mt9m001_g_volatile_ctrl,
  537. .s_ctrl = mt9m001_s_ctrl,
  538. };
  539. static const struct v4l2_subdev_core_ops mt9m001_subdev_core_ops = {
  540. .log_status = v4l2_ctrl_subdev_log_status,
  541. .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
  542. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  543. #ifdef CONFIG_VIDEO_ADV_DEBUG
  544. .g_register = mt9m001_g_register,
  545. .s_register = mt9m001_s_register,
  546. #endif
  547. };
  548. static int mt9m001_init_cfg(struct v4l2_subdev *sd,
  549. struct v4l2_subdev_state *sd_state)
  550. {
  551. struct i2c_client *client = v4l2_get_subdevdata(sd);
  552. struct mt9m001 *mt9m001 = to_mt9m001(client);
  553. struct v4l2_mbus_framefmt *try_fmt =
  554. v4l2_subdev_get_try_format(sd, sd_state, 0);
  555. try_fmt->width = MT9M001_MAX_WIDTH;
  556. try_fmt->height = MT9M001_MAX_HEIGHT;
  557. try_fmt->code = mt9m001->fmts[0].code;
  558. try_fmt->colorspace = mt9m001->fmts[0].colorspace;
  559. try_fmt->field = V4L2_FIELD_NONE;
  560. try_fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  561. try_fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
  562. try_fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
  563. return 0;
  564. }
  565. static int mt9m001_enum_mbus_code(struct v4l2_subdev *sd,
  566. struct v4l2_subdev_state *sd_state,
  567. struct v4l2_subdev_mbus_code_enum *code)
  568. {
  569. struct i2c_client *client = v4l2_get_subdevdata(sd);
  570. struct mt9m001 *mt9m001 = to_mt9m001(client);
  571. if (code->pad || code->index >= mt9m001->num_fmts)
  572. return -EINVAL;
  573. code->code = mt9m001->fmts[code->index].code;
  574. return 0;
  575. }
  576. static int mt9m001_get_mbus_config(struct v4l2_subdev *sd,
  577. unsigned int pad,
  578. struct v4l2_mbus_config *cfg)
  579. {
  580. /* MT9M001 has all capture_format parameters fixed */
  581. cfg->type = V4L2_MBUS_PARALLEL;
  582. cfg->bus.parallel.flags = V4L2_MBUS_PCLK_SAMPLE_FALLING |
  583. V4L2_MBUS_HSYNC_ACTIVE_HIGH |
  584. V4L2_MBUS_VSYNC_ACTIVE_HIGH |
  585. V4L2_MBUS_DATA_ACTIVE_HIGH |
  586. V4L2_MBUS_MASTER;
  587. return 0;
  588. }
  589. static const struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = {
  590. .s_stream = mt9m001_s_stream,
  591. };
  592. static const struct v4l2_subdev_sensor_ops mt9m001_subdev_sensor_ops = {
  593. .g_skip_top_lines = mt9m001_g_skip_top_lines,
  594. };
  595. static const struct v4l2_subdev_pad_ops mt9m001_subdev_pad_ops = {
  596. .init_cfg = mt9m001_init_cfg,
  597. .enum_mbus_code = mt9m001_enum_mbus_code,
  598. .get_selection = mt9m001_get_selection,
  599. .set_selection = mt9m001_set_selection,
  600. .get_fmt = mt9m001_get_fmt,
  601. .set_fmt = mt9m001_set_fmt,
  602. .get_mbus_config = mt9m001_get_mbus_config,
  603. };
  604. static const struct v4l2_subdev_ops mt9m001_subdev_ops = {
  605. .core = &mt9m001_subdev_core_ops,
  606. .video = &mt9m001_subdev_video_ops,
  607. .sensor = &mt9m001_subdev_sensor_ops,
  608. .pad = &mt9m001_subdev_pad_ops,
  609. };
  610. static int mt9m001_probe(struct i2c_client *client)
  611. {
  612. struct mt9m001 *mt9m001;
  613. struct i2c_adapter *adapter = client->adapter;
  614. int ret;
  615. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  616. dev_warn(&adapter->dev,
  617. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  618. return -EIO;
  619. }
  620. mt9m001 = devm_kzalloc(&client->dev, sizeof(*mt9m001), GFP_KERNEL);
  621. if (!mt9m001)
  622. return -ENOMEM;
  623. mt9m001->clk = devm_clk_get(&client->dev, NULL);
  624. if (IS_ERR(mt9m001->clk))
  625. return PTR_ERR(mt9m001->clk);
  626. mt9m001->standby_gpio = devm_gpiod_get_optional(&client->dev, "standby",
  627. GPIOD_OUT_LOW);
  628. if (IS_ERR(mt9m001->standby_gpio))
  629. return PTR_ERR(mt9m001->standby_gpio);
  630. mt9m001->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
  631. GPIOD_OUT_LOW);
  632. if (IS_ERR(mt9m001->reset_gpio))
  633. return PTR_ERR(mt9m001->reset_gpio);
  634. v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops);
  635. mt9m001->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
  636. V4L2_SUBDEV_FL_HAS_EVENTS;
  637. v4l2_ctrl_handler_init(&mt9m001->hdl, 4);
  638. v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
  639. V4L2_CID_VFLIP, 0, 1, 1, 0);
  640. v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
  641. V4L2_CID_GAIN, 0, 127, 1, 64);
  642. mt9m001->exposure = v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
  643. V4L2_CID_EXPOSURE, 1, 255, 1, 255);
  644. /*
  645. * Simulated autoexposure. If enabled, we calculate shutter width
  646. * ourselves in the driver based on vertical blanking and frame width
  647. */
  648. mt9m001->autoexposure = v4l2_ctrl_new_std_menu(&mt9m001->hdl,
  649. &mt9m001_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
  650. V4L2_EXPOSURE_AUTO);
  651. mt9m001->subdev.ctrl_handler = &mt9m001->hdl;
  652. if (mt9m001->hdl.error)
  653. return mt9m001->hdl.error;
  654. v4l2_ctrl_auto_cluster(2, &mt9m001->autoexposure,
  655. V4L2_EXPOSURE_MANUAL, true);
  656. mutex_init(&mt9m001->mutex);
  657. mt9m001->hdl.lock = &mt9m001->mutex;
  658. /* Second stage probe - when a capture adapter is there */
  659. mt9m001->y_skip_top = 0;
  660. mt9m001->rect.left = MT9M001_COLUMN_SKIP;
  661. mt9m001->rect.top = MT9M001_ROW_SKIP;
  662. mt9m001->rect.width = MT9M001_MAX_WIDTH;
  663. mt9m001->rect.height = MT9M001_MAX_HEIGHT;
  664. ret = mt9m001_power_on(&client->dev);
  665. if (ret)
  666. goto error_hdl_free;
  667. pm_runtime_set_active(&client->dev);
  668. pm_runtime_enable(&client->dev);
  669. ret = mt9m001_video_probe(client);
  670. if (ret)
  671. goto error_power_off;
  672. mt9m001->pad.flags = MEDIA_PAD_FL_SOURCE;
  673. mt9m001->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  674. ret = media_entity_pads_init(&mt9m001->subdev.entity, 1, &mt9m001->pad);
  675. if (ret)
  676. goto error_power_off;
  677. ret = v4l2_async_register_subdev(&mt9m001->subdev);
  678. if (ret)
  679. goto error_entity_cleanup;
  680. pm_runtime_idle(&client->dev);
  681. return 0;
  682. error_entity_cleanup:
  683. media_entity_cleanup(&mt9m001->subdev.entity);
  684. error_power_off:
  685. pm_runtime_disable(&client->dev);
  686. pm_runtime_set_suspended(&client->dev);
  687. mt9m001_power_off(&client->dev);
  688. error_hdl_free:
  689. v4l2_ctrl_handler_free(&mt9m001->hdl);
  690. mutex_destroy(&mt9m001->mutex);
  691. return ret;
  692. }
  693. static void mt9m001_remove(struct i2c_client *client)
  694. {
  695. struct mt9m001 *mt9m001 = to_mt9m001(client);
  696. /*
  697. * As it increments RPM usage_count even on errors, we don't need to
  698. * check the returned code here.
  699. */
  700. pm_runtime_get_sync(&client->dev);
  701. v4l2_async_unregister_subdev(&mt9m001->subdev);
  702. media_entity_cleanup(&mt9m001->subdev.entity);
  703. pm_runtime_disable(&client->dev);
  704. pm_runtime_set_suspended(&client->dev);
  705. pm_runtime_put_noidle(&client->dev);
  706. mt9m001_power_off(&client->dev);
  707. v4l2_ctrl_handler_free(&mt9m001->hdl);
  708. mutex_destroy(&mt9m001->mutex);
  709. }
  710. static const struct i2c_device_id mt9m001_id[] = {
  711. { "mt9m001", 0 },
  712. { }
  713. };
  714. MODULE_DEVICE_TABLE(i2c, mt9m001_id);
  715. static const struct dev_pm_ops mt9m001_pm_ops = {
  716. SET_RUNTIME_PM_OPS(mt9m001_power_off, mt9m001_power_on, NULL)
  717. };
  718. static const struct of_device_id mt9m001_of_match[] = {
  719. { .compatible = "onnn,mt9m001", },
  720. { /* sentinel */ },
  721. };
  722. MODULE_DEVICE_TABLE(of, mt9m001_of_match);
  723. static struct i2c_driver mt9m001_i2c_driver = {
  724. .driver = {
  725. .name = "mt9m001",
  726. .pm = &mt9m001_pm_ops,
  727. .of_match_table = mt9m001_of_match,
  728. },
  729. .probe_new = mt9m001_probe,
  730. .remove = mt9m001_remove,
  731. .id_table = mt9m001_id,
  732. };
  733. module_i2c_driver(mt9m001_i2c_driver);
  734. MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
  735. MODULE_AUTHOR("Guennadi Liakhovetski <[email protected]>");
  736. MODULE_LICENSE("GPL v2");