mt9m001.c 23 KB

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