1 From d13c94482be9ca356df8a04f8fd5f3738dc31ab1 Mon Sep 17 00:00:00 2001
2 From: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
3 Date: Fri, 4 Oct 2019 13:05:25 -0300
4 Subject: [PATCH] media: i2c: Add IMX290 CMOS image sensor driver
6 Commit 828dbc299278065b634e913d2700d254a3224853 upstream.
8 Add driver for Sony IMX290 CMOS image sensor driver. The driver only
9 supports I2C interface for programming and MIPI CSI-2 for sensor output.
11 [Sakari Ailus: Rewrapped a few lines over 80 chars a little.]
13 Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
14 Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
15 Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
17 drivers/media/i2c/Kconfig | 11 +
18 drivers/media/i2c/Makefile | 1 +
19 drivers/media/i2c/imx290.c | 884 +++++++++++++++++++++++++++++++++++++
20 3 files changed, 896 insertions(+)
21 create mode 100644 drivers/media/i2c/imx290.c
23 --- a/drivers/media/i2c/Kconfig
24 +++ b/drivers/media/i2c/Kconfig
25 @@ -609,6 +609,17 @@ config VIDEO_IMX274
26 This is a V4L2 sensor driver for the Sony IMX274
30 + tristate "Sony IMX290 sensor support"
31 + depends on I2C && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
34 + This is a Video4Linux2 sensor driver for the Sony
35 + IMX290 camera sensor.
37 + To compile this driver as a module, choose M here: the
38 + module will be called imx290.
41 tristate "Sony IMX477 sensor support"
42 depends on I2C && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
43 --- a/drivers/media/i2c/Makefile
44 +++ b/drivers/media/i2c/Makefile
45 @@ -114,6 +114,7 @@ obj-$(CONFIG_VIDEO_IMX214) += imx214.o
46 obj-$(CONFIG_VIDEO_IMX219) += imx219.o
47 obj-$(CONFIG_VIDEO_IMX258) += imx258.o
48 obj-$(CONFIG_VIDEO_IMX274) += imx274.o
49 +obj-$(CONFIG_VIDEO_IMX290) += imx290.o
50 obj-$(CONFIG_VIDEO_IMX477) += imx477.o
51 obj-$(CONFIG_VIDEO_IMX319) += imx319.o
52 obj-$(CONFIG_VIDEO_IMX355) += imx355.o
54 +++ b/drivers/media/i2c/imx290.c
56 +// SPDX-License-Identifier: GPL-2.0
58 + * Sony IMX290 CMOS Image Sensor Driver
60 + * Copyright (C) 2019 FRAMOS GmbH.
62 + * Copyright (C) 2019 Linaro Ltd.
63 + * Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
66 +#include <linux/clk.h>
67 +#include <linux/delay.h>
68 +#include <linux/gpio/consumer.h>
69 +#include <linux/i2c.h>
70 +#include <linux/module.h>
71 +#include <linux/pm_runtime.h>
72 +#include <linux/regmap.h>
73 +#include <linux/regulator/consumer.h>
74 +#include <media/media-entity.h>
75 +#include <media/v4l2-ctrls.h>
76 +#include <media/v4l2-device.h>
77 +#include <media/v4l2-fwnode.h>
78 +#include <media/v4l2-subdev.h>
80 +#define IMX290_STANDBY 0x3000
81 +#define IMX290_REGHOLD 0x3001
82 +#define IMX290_XMSTA 0x3002
83 +#define IMX290_GAIN 0x3014
85 +#define IMX290_DEFAULT_LINK_FREQ 445500000
87 +static const char * const imx290_supply_name[] = {
93 +#define IMX290_NUM_SUPPLIES ARRAY_SIZE(imx290_supply_name)
95 +struct imx290_regval {
100 +struct imx290_mode {
104 + u32 link_freq_index;
106 + const struct imx290_regval *data;
111 + struct device *dev;
113 + struct regmap *regmap;
115 + struct v4l2_subdev sd;
116 + struct v4l2_fwnode_endpoint ep;
117 + struct media_pad pad;
118 + struct v4l2_mbus_framefmt current_format;
119 + const struct imx290_mode *current_mode;
121 + struct regulator_bulk_data supplies[IMX290_NUM_SUPPLIES];
122 + struct gpio_desc *rst_gpio;
124 + struct v4l2_ctrl_handler ctrls;
125 + struct v4l2_ctrl *link_freq;
126 + struct v4l2_ctrl *pixel_rate;
131 +struct imx290_pixfmt {
135 +static const struct imx290_pixfmt imx290_formats[] = {
136 + { MEDIA_BUS_FMT_SRGGB10_1X10 },
139 +static const struct regmap_config imx290_regmap_config = {
142 + .cache_type = REGCACHE_RBTREE,
145 +static const struct imx290_regval imx290_global_init_settings[] = {
208 +static const struct imx290_regval imx290_1080p_settings[] = {
209 + /* mode settings */
226 + /* data rate settings */
249 +static const struct imx290_regval imx290_720p_settings[] = {
250 + /* mode settings */
267 + /* data rate settings */
290 +static const struct imx290_regval imx290_10bit_settings[] = {
302 +/* supported link frequencies */
303 +static const s64 imx290_link_freq[] = {
304 + IMX290_DEFAULT_LINK_FREQ,
308 +static const struct imx290_mode imx290_modes[] = {
312 + .data = imx290_1080p_settings,
313 + .data_size = ARRAY_SIZE(imx290_1080p_settings),
314 + .pixel_rate = 178200000,
315 + .link_freq_index = 0,
320 + .data = imx290_720p_settings,
321 + .data_size = ARRAY_SIZE(imx290_720p_settings),
322 + .pixel_rate = 178200000,
323 + .link_freq_index = 0,
327 +static inline struct imx290 *to_imx290(struct v4l2_subdev *_sd)
329 + return container_of(_sd, struct imx290, sd);
332 +static inline int imx290_read_reg(struct imx290 *imx290, u16 addr, u8 *value)
334 + unsigned int regval;
337 + ret = regmap_read(imx290->regmap, addr, ®val);
339 + dev_err(imx290->dev, "I2C read failed for addr: %x\n", addr);
343 + *value = regval & 0xff;
348 +static int imx290_write_reg(struct imx290 *imx290, u16 addr, u8 value)
352 + ret = regmap_write(imx290->regmap, addr, value);
354 + dev_err(imx290->dev, "I2C write failed for addr: %x\n", addr);
361 +static int imx290_set_register_array(struct imx290 *imx290,
362 + const struct imx290_regval *settings,
363 + unsigned int num_settings)
368 + for (i = 0; i < num_settings; ++i, ++settings) {
369 + ret = imx290_write_reg(imx290, settings->reg, settings->val);
373 + /* Settle time is 10ms for all registers */
380 +static int imx290_write_buffered_reg(struct imx290 *imx290, u16 address_low,
381 + u8 nr_regs, u32 value)
386 + ret = imx290_write_reg(imx290, IMX290_REGHOLD, 0x01);
388 + dev_err(imx290->dev, "Error setting hold register\n");
392 + for (i = 0; i < nr_regs; i++) {
393 + ret = imx290_write_reg(imx290, address_low + i,
394 + (u8)(value >> (i * 8)));
396 + dev_err(imx290->dev, "Error writing buffered registers\n");
401 + ret = imx290_write_reg(imx290, IMX290_REGHOLD, 0x00);
403 + dev_err(imx290->dev, "Error setting hold register\n");
410 +static int imx290_set_gain(struct imx290 *imx290, u32 value)
414 + ret = imx290_write_buffered_reg(imx290, IMX290_GAIN, 1, value);
416 + dev_err(imx290->dev, "Unable to write gain\n");
421 +/* Stop streaming */
422 +static int imx290_stop_streaming(struct imx290 *imx290)
426 + ret = imx290_write_reg(imx290, IMX290_STANDBY, 0x01);
432 + return imx290_write_reg(imx290, IMX290_XMSTA, 0x01);
435 +static int imx290_set_ctrl(struct v4l2_ctrl *ctrl)
437 + struct imx290 *imx290 = container_of(ctrl->handler,
438 + struct imx290, ctrls);
441 + /* V4L2 controls values will be applied only when power is already up */
442 + if (!pm_runtime_get_if_in_use(imx290->dev))
445 + switch (ctrl->id) {
446 + case V4L2_CID_GAIN:
447 + ret = imx290_set_gain(imx290, ctrl->val);
454 + pm_runtime_put(imx290->dev);
459 +static const struct v4l2_ctrl_ops imx290_ctrl_ops = {
460 + .s_ctrl = imx290_set_ctrl,
463 +static int imx290_enum_mbus_code(struct v4l2_subdev *sd,
464 + struct v4l2_subdev_pad_config *cfg,
465 + struct v4l2_subdev_mbus_code_enum *code)
467 + if (code->index >= ARRAY_SIZE(imx290_formats))
470 + code->code = imx290_formats[code->index].code;
475 +static int imx290_get_fmt(struct v4l2_subdev *sd,
476 + struct v4l2_subdev_pad_config *cfg,
477 + struct v4l2_subdev_format *fmt)
479 + struct imx290 *imx290 = to_imx290(sd);
480 + struct v4l2_mbus_framefmt *framefmt;
482 + mutex_lock(&imx290->lock);
484 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
485 + framefmt = v4l2_subdev_get_try_format(&imx290->sd, cfg,
488 + framefmt = &imx290->current_format;
490 + fmt->format = *framefmt;
492 + mutex_unlock(&imx290->lock);
497 +static int imx290_set_fmt(struct v4l2_subdev *sd,
498 + struct v4l2_subdev_pad_config *cfg,
499 + struct v4l2_subdev_format *fmt)
501 + struct imx290 *imx290 = to_imx290(sd);
502 + const struct imx290_mode *mode;
503 + struct v4l2_mbus_framefmt *format;
506 + mutex_lock(&imx290->lock);
508 + mode = v4l2_find_nearest_size(imx290_modes,
509 + ARRAY_SIZE(imx290_modes),
511 + fmt->format.width, fmt->format.height);
513 + fmt->format.width = mode->width;
514 + fmt->format.height = mode->height;
516 + for (i = 0; i < ARRAY_SIZE(imx290_formats); i++)
517 + if (imx290_formats[i].code == fmt->format.code)
520 + if (i >= ARRAY_SIZE(imx290_formats))
523 + fmt->format.code = imx290_formats[i].code;
524 + fmt->format.field = V4L2_FIELD_NONE;
526 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
527 + format = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
529 + format = &imx290->current_format;
530 + __v4l2_ctrl_s_ctrl(imx290->link_freq, mode->link_freq_index);
531 + __v4l2_ctrl_s_ctrl_int64(imx290->pixel_rate, mode->pixel_rate);
533 + imx290->current_mode = mode;
536 + *format = fmt->format;
538 + mutex_unlock(&imx290->lock);
543 +static int imx290_entity_init_cfg(struct v4l2_subdev *subdev,
544 + struct v4l2_subdev_pad_config *cfg)
546 + struct v4l2_subdev_format fmt = { 0 };
548 + fmt.which = cfg ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
549 + fmt.format.width = 1920;
550 + fmt.format.height = 1080;
552 + imx290_set_fmt(subdev, cfg, &fmt);
557 +static int imx290_write_current_format(struct imx290 *imx290,
558 + struct v4l2_mbus_framefmt *format)
562 + switch (format->code) {
563 + case MEDIA_BUS_FMT_SRGGB10_1X10:
564 + ret = imx290_set_register_array(imx290, imx290_10bit_settings,
566 + imx290_10bit_settings));
568 + dev_err(imx290->dev, "Could not set format registers\n");
573 + dev_err(imx290->dev, "Unknown pixel format\n");
580 +/* Start streaming */
581 +static int imx290_start_streaming(struct imx290 *imx290)
585 + /* Set init register settings */
586 + ret = imx290_set_register_array(imx290, imx290_global_init_settings,
588 + imx290_global_init_settings));
590 + dev_err(imx290->dev, "Could not set init registers\n");
594 + /* Set current frame format */
595 + ret = imx290_write_current_format(imx290, &imx290->current_format);
597 + dev_err(imx290->dev, "Could not set frame format\n");
601 + /* Apply default values of current mode */
602 + ret = imx290_set_register_array(imx290, imx290->current_mode->data,
603 + imx290->current_mode->data_size);
605 + dev_err(imx290->dev, "Could not set current mode\n");
609 + /* Apply customized values from user */
610 + ret = v4l2_ctrl_handler_setup(imx290->sd.ctrl_handler);
612 + dev_err(imx290->dev, "Could not sync v4l2 controls\n");
616 + ret = imx290_write_reg(imx290, IMX290_STANDBY, 0x00);
622 + /* Start streaming */
623 + return imx290_write_reg(imx290, IMX290_XMSTA, 0x00);
626 +static int imx290_set_stream(struct v4l2_subdev *sd, int enable)
628 + struct imx290 *imx290 = to_imx290(sd);
632 + ret = pm_runtime_get_sync(imx290->dev);
634 + pm_runtime_put_noidle(imx290->dev);
635 + goto unlock_and_return;
638 + ret = imx290_start_streaming(imx290);
640 + dev_err(imx290->dev, "Start stream failed\n");
641 + pm_runtime_put(imx290->dev);
642 + goto unlock_and_return;
645 + imx290_stop_streaming(imx290);
646 + pm_runtime_put(imx290->dev);
654 +static int imx290_get_regulators(struct device *dev, struct imx290 *imx290)
658 + for (i = 0; i < IMX290_NUM_SUPPLIES; i++)
659 + imx290->supplies[i].supply = imx290_supply_name[i];
661 + return devm_regulator_bulk_get(dev, IMX290_NUM_SUPPLIES,
665 +static int imx290_power_on(struct device *dev)
667 + struct i2c_client *client = to_i2c_client(dev);
668 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
669 + struct imx290 *imx290 = to_imx290(sd);
672 + ret = clk_prepare_enable(imx290->xclk);
674 + dev_err(imx290->dev, "Failed to enable clock\n");
678 + ret = regulator_bulk_enable(IMX290_NUM_SUPPLIES, imx290->supplies);
680 + dev_err(imx290->dev, "Failed to enable regulators\n");
681 + clk_disable_unprepare(imx290->xclk);
685 + usleep_range(1, 2);
686 + gpiod_set_value_cansleep(imx290->rst_gpio, 1);
687 + usleep_range(30000, 31000);
692 +static int imx290_power_off(struct device *dev)
694 + struct i2c_client *client = to_i2c_client(dev);
695 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
696 + struct imx290 *imx290 = to_imx290(sd);
698 + clk_disable_unprepare(imx290->xclk);
699 + gpiod_set_value_cansleep(imx290->rst_gpio, 0);
700 + regulator_bulk_disable(IMX290_NUM_SUPPLIES, imx290->supplies);
705 +static const struct dev_pm_ops imx290_pm_ops = {
706 + SET_RUNTIME_PM_OPS(imx290_power_on, imx290_power_off, NULL)
709 +static const struct v4l2_subdev_video_ops imx290_video_ops = {
710 + .s_stream = imx290_set_stream,
713 +static const struct v4l2_subdev_pad_ops imx290_pad_ops = {
714 + .init_cfg = imx290_entity_init_cfg,
715 + .enum_mbus_code = imx290_enum_mbus_code,
716 + .get_fmt = imx290_get_fmt,
717 + .set_fmt = imx290_set_fmt,
720 +static const struct v4l2_subdev_ops imx290_subdev_ops = {
721 + .video = &imx290_video_ops,
722 + .pad = &imx290_pad_ops,
725 +static const struct media_entity_operations imx290_subdev_entity_ops = {
726 + .link_validate = v4l2_subdev_link_validate,
729 +static int imx290_probe(struct i2c_client *client)
731 + struct device *dev = &client->dev;
732 + struct fwnode_handle *endpoint;
733 + struct imx290 *imx290;
737 + imx290 = devm_kzalloc(dev, sizeof(*imx290), GFP_KERNEL);
742 + imx290->regmap = devm_regmap_init_i2c(client, &imx290_regmap_config);
743 + if (IS_ERR(imx290->regmap)) {
744 + dev_err(dev, "Unable to initialize I2C\n");
748 + endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
750 + dev_err(dev, "Endpoint node not found\n");
754 + ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &imx290->ep);
755 + fwnode_handle_put(endpoint);
757 + dev_err(dev, "Parsing endpoint node failed\n");
761 + if (!imx290->ep.nr_of_link_frequencies) {
762 + dev_err(dev, "link-frequency property not found in DT\n");
767 + if (imx290->ep.link_frequencies[0] != IMX290_DEFAULT_LINK_FREQ) {
768 + dev_err(dev, "Unsupported link frequency\n");
773 + /* Only CSI2 is supported for now */
774 + if (imx290->ep.bus_type != V4L2_MBUS_CSI2_DPHY) {
775 + dev_err(dev, "Unsupported bus type, should be CSI2\n");
780 + /* Set default mode to max resolution */
781 + imx290->current_mode = &imx290_modes[0];
783 + /* get system clock (xclk) */
784 + imx290->xclk = devm_clk_get(dev, "xclk");
785 + if (IS_ERR(imx290->xclk)) {
786 + dev_err(dev, "Could not get xclk");
787 + ret = PTR_ERR(imx290->xclk);
791 + ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
794 + dev_err(dev, "Could not get xclk frequency\n");
798 + /* external clock must be 37.125 MHz */
799 + if (xclk_freq != 37125000) {
800 + dev_err(dev, "External clock frequency %u is not supported\n",
806 + ret = clk_set_rate(imx290->xclk, xclk_freq);
808 + dev_err(dev, "Could not set xclk frequency\n");
812 + ret = imx290_get_regulators(dev, imx290);
814 + dev_err(dev, "Cannot get regulators\n");
818 + imx290->rst_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
819 + if (IS_ERR(imx290->rst_gpio)) {
820 + dev_err(dev, "Cannot get reset gpio\n");
821 + ret = PTR_ERR(imx290->rst_gpio);
825 + mutex_init(&imx290->lock);
827 + v4l2_ctrl_handler_init(&imx290->ctrls, 3);
829 + v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
830 + V4L2_CID_GAIN, 0, 72, 1, 0);
831 + imx290->link_freq =
832 + v4l2_ctrl_new_int_menu(&imx290->ctrls,
834 + V4L2_CID_LINK_FREQ,
835 + ARRAY_SIZE(imx290_link_freq) - 1,
836 + 0, imx290_link_freq);
837 + if (imx290->link_freq)
838 + imx290->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
840 + imx290->pixel_rate = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
841 + V4L2_CID_PIXEL_RATE, 1,
843 + imx290_modes[0].pixel_rate);
845 + imx290->sd.ctrl_handler = &imx290->ctrls;
847 + if (imx290->ctrls.error) {
848 + dev_err(dev, "Control initialization error %d\n",
849 + imx290->ctrls.error);
850 + ret = imx290->ctrls.error;
854 + v4l2_i2c_subdev_init(&imx290->sd, client, &imx290_subdev_ops);
855 + imx290->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
856 + imx290->sd.dev = &client->dev;
857 + imx290->sd.entity.ops = &imx290_subdev_entity_ops;
858 + imx290->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
860 + imx290->pad.flags = MEDIA_PAD_FL_SOURCE;
861 + ret = media_entity_pads_init(&imx290->sd.entity, 1, &imx290->pad);
863 + dev_err(dev, "Could not register media entity\n");
867 + ret = v4l2_async_register_subdev(&imx290->sd);
869 + dev_err(dev, "Could not register v4l2 device\n");
873 + /* Power on the device to match runtime PM state below */
874 + ret = imx290_power_on(dev);
876 + dev_err(dev, "Could not power on the device\n");
880 + pm_runtime_set_active(dev);
881 + pm_runtime_enable(dev);
882 + pm_runtime_idle(dev);
884 + v4l2_fwnode_endpoint_free(&imx290->ep);
889 + media_entity_cleanup(&imx290->sd.entity);
891 + v4l2_ctrl_handler_free(&imx290->ctrls);
892 + mutex_destroy(&imx290->lock);
894 + v4l2_fwnode_endpoint_free(&imx290->ep);
899 +static int imx290_remove(struct i2c_client *client)
901 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
902 + struct imx290 *imx290 = to_imx290(sd);
904 + v4l2_async_unregister_subdev(sd);
905 + media_entity_cleanup(&sd->entity);
906 + v4l2_ctrl_handler_free(sd->ctrl_handler);
908 + mutex_destroy(&imx290->lock);
910 + pm_runtime_disable(imx290->dev);
911 + if (!pm_runtime_status_suspended(imx290->dev))
912 + imx290_power_off(imx290->dev);
913 + pm_runtime_set_suspended(imx290->dev);
918 +static const struct of_device_id imx290_of_match[] = {
919 + { .compatible = "sony,imx290" },
922 +MODULE_DEVICE_TABLE(of, imx290_of_match);
924 +static struct i2c_driver imx290_i2c_driver = {
925 + .probe_new = imx290_probe,
926 + .remove = imx290_remove,
929 + .pm = &imx290_pm_ops,
930 + .of_match_table = of_match_ptr(imx290_of_match),
934 +module_i2c_driver(imx290_i2c_driver);
936 +MODULE_DESCRIPTION("Sony IMX290 CMOS Image Sensor Driver");
937 +MODULE_AUTHOR("FRAMOS GmbH");
938 +MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
939 +MODULE_LICENSE("GPL v2");