Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * imx214.c - imx214 sensor driver
0004  *
0005  * Copyright 2018 Qtechnology A/S
0006  *
0007  * Ricardo Ribalda <ribalda@kernel.org>
0008  */
0009 #include <linux/clk.h>
0010 #include <linux/delay.h>
0011 #include <linux/gpio/consumer.h>
0012 #include <linux/i2c.h>
0013 #include <linux/module.h>
0014 #include <linux/pm_runtime.h>
0015 #include <linux/regmap.h>
0016 #include <linux/regulator/consumer.h>
0017 #include <media/media-entity.h>
0018 #include <media/v4l2-ctrls.h>
0019 #include <media/v4l2-fwnode.h>
0020 #include <media/v4l2-subdev.h>
0021 
0022 #define IMX214_DEFAULT_CLK_FREQ 24000000
0023 #define IMX214_DEFAULT_LINK_FREQ 480000000
0024 #define IMX214_DEFAULT_PIXEL_RATE ((IMX214_DEFAULT_LINK_FREQ * 8LL) / 10)
0025 #define IMX214_FPS 30
0026 #define IMX214_MBUS_CODE MEDIA_BUS_FMT_SRGGB10_1X10
0027 
0028 static const char * const imx214_supply_name[] = {
0029     "vdda",
0030     "vddd",
0031     "vdddo",
0032 };
0033 
0034 #define IMX214_NUM_SUPPLIES ARRAY_SIZE(imx214_supply_name)
0035 
0036 struct imx214 {
0037     struct device *dev;
0038     struct clk *xclk;
0039     struct regmap *regmap;
0040 
0041     struct v4l2_subdev sd;
0042     struct media_pad pad;
0043     struct v4l2_mbus_framefmt fmt;
0044     struct v4l2_rect crop;
0045 
0046     struct v4l2_ctrl_handler ctrls;
0047     struct v4l2_ctrl *pixel_rate;
0048     struct v4l2_ctrl *link_freq;
0049     struct v4l2_ctrl *exposure;
0050     struct v4l2_ctrl *unit_size;
0051 
0052     struct regulator_bulk_data  supplies[IMX214_NUM_SUPPLIES];
0053 
0054     struct gpio_desc *enable_gpio;
0055 
0056     /*
0057      * Serialize control access, get/set format, get selection
0058      * and start streaming.
0059      */
0060     struct mutex mutex;
0061 
0062     bool streaming;
0063 };
0064 
0065 struct reg_8 {
0066     u16 addr;
0067     u8 val;
0068 };
0069 
0070 enum {
0071     IMX214_TABLE_WAIT_MS = 0,
0072     IMX214_TABLE_END,
0073     IMX214_MAX_RETRIES,
0074     IMX214_WAIT_MS
0075 };
0076 
0077 /*From imx214_mode_tbls.h*/
0078 static const struct reg_8 mode_4096x2304[] = {
0079     {0x0114, 0x03},
0080     {0x0220, 0x00},
0081     {0x0221, 0x11},
0082     {0x0222, 0x01},
0083     {0x0340, 0x0C},
0084     {0x0341, 0x7A},
0085     {0x0342, 0x13},
0086     {0x0343, 0x90},
0087     {0x0344, 0x00},
0088     {0x0345, 0x38},
0089     {0x0346, 0x01},
0090     {0x0347, 0x98},
0091     {0x0348, 0x10},
0092     {0x0349, 0x37},
0093     {0x034A, 0x0A},
0094     {0x034B, 0x97},
0095     {0x0381, 0x01},
0096     {0x0383, 0x01},
0097     {0x0385, 0x01},
0098     {0x0387, 0x01},
0099     {0x0900, 0x00},
0100     {0x0901, 0x00},
0101     {0x0902, 0x00},
0102     {0x3000, 0x35},
0103     {0x3054, 0x01},
0104     {0x305C, 0x11},
0105 
0106     {0x0112, 0x0A},
0107     {0x0113, 0x0A},
0108     {0x034C, 0x10},
0109     {0x034D, 0x00},
0110     {0x034E, 0x09},
0111     {0x034F, 0x00},
0112     {0x0401, 0x00},
0113     {0x0404, 0x00},
0114     {0x0405, 0x10},
0115     {0x0408, 0x00},
0116     {0x0409, 0x00},
0117     {0x040A, 0x00},
0118     {0x040B, 0x00},
0119     {0x040C, 0x10},
0120     {0x040D, 0x00},
0121     {0x040E, 0x09},
0122     {0x040F, 0x00},
0123 
0124     {0x0301, 0x05},
0125     {0x0303, 0x02},
0126     {0x0305, 0x03},
0127     {0x0306, 0x00},
0128     {0x0307, 0x96},
0129     {0x0309, 0x0A},
0130     {0x030B, 0x01},
0131     {0x0310, 0x00},
0132 
0133     {0x0820, 0x12},
0134     {0x0821, 0xC0},
0135     {0x0822, 0x00},
0136     {0x0823, 0x00},
0137 
0138     {0x3A03, 0x09},
0139     {0x3A04, 0x50},
0140     {0x3A05, 0x01},
0141 
0142     {0x0B06, 0x01},
0143     {0x30A2, 0x00},
0144 
0145     {0x30B4, 0x00},
0146 
0147     {0x3A02, 0xFF},
0148 
0149     {0x3011, 0x00},
0150     {0x3013, 0x01},
0151 
0152     {0x0202, 0x0C},
0153     {0x0203, 0x70},
0154     {0x0224, 0x01},
0155     {0x0225, 0xF4},
0156 
0157     {0x0204, 0x00},
0158     {0x0205, 0x00},
0159     {0x020E, 0x01},
0160     {0x020F, 0x00},
0161     {0x0210, 0x01},
0162     {0x0211, 0x00},
0163     {0x0212, 0x01},
0164     {0x0213, 0x00},
0165     {0x0214, 0x01},
0166     {0x0215, 0x00},
0167     {0x0216, 0x00},
0168     {0x0217, 0x00},
0169 
0170     {0x4170, 0x00},
0171     {0x4171, 0x10},
0172     {0x4176, 0x00},
0173     {0x4177, 0x3C},
0174     {0xAE20, 0x04},
0175     {0xAE21, 0x5C},
0176 
0177     {IMX214_TABLE_WAIT_MS, 10},
0178     {0x0138, 0x01},
0179     {IMX214_TABLE_END, 0x00}
0180 };
0181 
0182 static const struct reg_8 mode_1920x1080[] = {
0183     {0x0114, 0x03},
0184     {0x0220, 0x00},
0185     {0x0221, 0x11},
0186     {0x0222, 0x01},
0187     {0x0340, 0x0C},
0188     {0x0341, 0x7A},
0189     {0x0342, 0x13},
0190     {0x0343, 0x90},
0191     {0x0344, 0x04},
0192     {0x0345, 0x78},
0193     {0x0346, 0x03},
0194     {0x0347, 0xFC},
0195     {0x0348, 0x0B},
0196     {0x0349, 0xF7},
0197     {0x034A, 0x08},
0198     {0x034B, 0x33},
0199     {0x0381, 0x01},
0200     {0x0383, 0x01},
0201     {0x0385, 0x01},
0202     {0x0387, 0x01},
0203     {0x0900, 0x00},
0204     {0x0901, 0x00},
0205     {0x0902, 0x00},
0206     {0x3000, 0x35},
0207     {0x3054, 0x01},
0208     {0x305C, 0x11},
0209 
0210     {0x0112, 0x0A},
0211     {0x0113, 0x0A},
0212     {0x034C, 0x07},
0213     {0x034D, 0x80},
0214     {0x034E, 0x04},
0215     {0x034F, 0x38},
0216     {0x0401, 0x00},
0217     {0x0404, 0x00},
0218     {0x0405, 0x10},
0219     {0x0408, 0x00},
0220     {0x0409, 0x00},
0221     {0x040A, 0x00},
0222     {0x040B, 0x00},
0223     {0x040C, 0x07},
0224     {0x040D, 0x80},
0225     {0x040E, 0x04},
0226     {0x040F, 0x38},
0227 
0228     {0x0301, 0x05},
0229     {0x0303, 0x02},
0230     {0x0305, 0x03},
0231     {0x0306, 0x00},
0232     {0x0307, 0x96},
0233     {0x0309, 0x0A},
0234     {0x030B, 0x01},
0235     {0x0310, 0x00},
0236 
0237     {0x0820, 0x12},
0238     {0x0821, 0xC0},
0239     {0x0822, 0x00},
0240     {0x0823, 0x00},
0241 
0242     {0x3A03, 0x04},
0243     {0x3A04, 0xF8},
0244     {0x3A05, 0x02},
0245 
0246     {0x0B06, 0x01},
0247     {0x30A2, 0x00},
0248 
0249     {0x30B4, 0x00},
0250 
0251     {0x3A02, 0xFF},
0252 
0253     {0x3011, 0x00},
0254     {0x3013, 0x01},
0255 
0256     {0x0202, 0x0C},
0257     {0x0203, 0x70},
0258     {0x0224, 0x01},
0259     {0x0225, 0xF4},
0260 
0261     {0x0204, 0x00},
0262     {0x0205, 0x00},
0263     {0x020E, 0x01},
0264     {0x020F, 0x00},
0265     {0x0210, 0x01},
0266     {0x0211, 0x00},
0267     {0x0212, 0x01},
0268     {0x0213, 0x00},
0269     {0x0214, 0x01},
0270     {0x0215, 0x00},
0271     {0x0216, 0x00},
0272     {0x0217, 0x00},
0273 
0274     {0x4170, 0x00},
0275     {0x4171, 0x10},
0276     {0x4176, 0x00},
0277     {0x4177, 0x3C},
0278     {0xAE20, 0x04},
0279     {0xAE21, 0x5C},
0280 
0281     {IMX214_TABLE_WAIT_MS, 10},
0282     {0x0138, 0x01},
0283     {IMX214_TABLE_END, 0x00}
0284 };
0285 
0286 static const struct reg_8 mode_table_common[] = {
0287     /* software reset */
0288 
0289     /* software standby settings */
0290     {0x0100, 0x00},
0291 
0292     /* ATR setting */
0293     {0x9300, 0x02},
0294 
0295     /* external clock setting */
0296     {0x0136, 0x18},
0297     {0x0137, 0x00},
0298 
0299     /* global setting */
0300     /* basic config */
0301     {0x0101, 0x00},
0302     {0x0105, 0x01},
0303     {0x0106, 0x01},
0304     {0x4550, 0x02},
0305     {0x4601, 0x00},
0306     {0x4642, 0x05},
0307     {0x6227, 0x11},
0308     {0x6276, 0x00},
0309     {0x900E, 0x06},
0310     {0xA802, 0x90},
0311     {0xA803, 0x11},
0312     {0xA804, 0x62},
0313     {0xA805, 0x77},
0314     {0xA806, 0xAE},
0315     {0xA807, 0x34},
0316     {0xA808, 0xAE},
0317     {0xA809, 0x35},
0318     {0xA80A, 0x62},
0319     {0xA80B, 0x83},
0320     {0xAE33, 0x00},
0321 
0322     /* analog setting */
0323     {0x4174, 0x00},
0324     {0x4175, 0x11},
0325     {0x4612, 0x29},
0326     {0x461B, 0x12},
0327     {0x461F, 0x06},
0328     {0x4635, 0x07},
0329     {0x4637, 0x30},
0330     {0x463F, 0x18},
0331     {0x4641, 0x0D},
0332     {0x465B, 0x12},
0333     {0x465F, 0x11},
0334     {0x4663, 0x11},
0335     {0x4667, 0x0F},
0336     {0x466F, 0x0F},
0337     {0x470E, 0x09},
0338     {0x4909, 0xAB},
0339     {0x490B, 0x95},
0340     {0x4915, 0x5D},
0341     {0x4A5F, 0xFF},
0342     {0x4A61, 0xFF},
0343     {0x4A73, 0x62},
0344     {0x4A85, 0x00},
0345     {0x4A87, 0xFF},
0346 
0347     /* embedded data */
0348     {0x5041, 0x04},
0349     {0x583C, 0x04},
0350     {0x620E, 0x04},
0351     {0x6EB2, 0x01},
0352     {0x6EB3, 0x00},
0353     {0x9300, 0x02},
0354 
0355     /* imagequality */
0356     /* HDR setting */
0357     {0x3001, 0x07},
0358     {0x6D12, 0x3F},
0359     {0x6D13, 0xFF},
0360     {0x9344, 0x03},
0361     {0x9706, 0x10},
0362     {0x9707, 0x03},
0363     {0x9708, 0x03},
0364     {0x9E04, 0x01},
0365     {0x9E05, 0x00},
0366     {0x9E0C, 0x01},
0367     {0x9E0D, 0x02},
0368     {0x9E24, 0x00},
0369     {0x9E25, 0x8C},
0370     {0x9E26, 0x00},
0371     {0x9E27, 0x94},
0372     {0x9E28, 0x00},
0373     {0x9E29, 0x96},
0374 
0375     /* CNR parameter setting */
0376     {0x69DB, 0x01},
0377 
0378     /* Moire reduction */
0379     {0x6957, 0x01},
0380 
0381     /* image enhancement */
0382     {0x6987, 0x17},
0383     {0x698A, 0x03},
0384     {0x698B, 0x03},
0385 
0386     /* white balanace */
0387     {0x0B8E, 0x01},
0388     {0x0B8F, 0x00},
0389     {0x0B90, 0x01},
0390     {0x0B91, 0x00},
0391     {0x0B92, 0x01},
0392     {0x0B93, 0x00},
0393     {0x0B94, 0x01},
0394     {0x0B95, 0x00},
0395 
0396     /* ATR setting */
0397     {0x6E50, 0x00},
0398     {0x6E51, 0x32},
0399     {0x9340, 0x00},
0400     {0x9341, 0x3C},
0401     {0x9342, 0x03},
0402     {0x9343, 0xFF},
0403     {IMX214_TABLE_END, 0x00}
0404 };
0405 
0406 /*
0407  * Declare modes in order, from biggest
0408  * to smallest height.
0409  */
0410 static const struct imx214_mode {
0411     u32 width;
0412     u32 height;
0413     const struct reg_8 *reg_table;
0414 } imx214_modes[] = {
0415     {
0416         .width = 4096,
0417         .height = 2304,
0418         .reg_table = mode_4096x2304,
0419     },
0420     {
0421         .width = 1920,
0422         .height = 1080,
0423         .reg_table = mode_1920x1080,
0424     },
0425 };
0426 
0427 static inline struct imx214 *to_imx214(struct v4l2_subdev *sd)
0428 {
0429     return container_of(sd, struct imx214, sd);
0430 }
0431 
0432 static int __maybe_unused imx214_power_on(struct device *dev)
0433 {
0434     struct i2c_client *client = to_i2c_client(dev);
0435     struct v4l2_subdev *sd = i2c_get_clientdata(client);
0436     struct imx214 *imx214 = to_imx214(sd);
0437     int ret;
0438 
0439     ret = regulator_bulk_enable(IMX214_NUM_SUPPLIES, imx214->supplies);
0440     if (ret < 0) {
0441         dev_err(imx214->dev, "failed to enable regulators: %d\n", ret);
0442         return ret;
0443     }
0444 
0445     usleep_range(2000, 3000);
0446 
0447     ret = clk_prepare_enable(imx214->xclk);
0448     if (ret < 0) {
0449         regulator_bulk_disable(IMX214_NUM_SUPPLIES, imx214->supplies);
0450         dev_err(imx214->dev, "clk prepare enable failed\n");
0451         return ret;
0452     }
0453 
0454     gpiod_set_value_cansleep(imx214->enable_gpio, 1);
0455     usleep_range(12000, 15000);
0456 
0457     return 0;
0458 }
0459 
0460 static int __maybe_unused imx214_power_off(struct device *dev)
0461 {
0462     struct i2c_client *client = to_i2c_client(dev);
0463     struct v4l2_subdev *sd = i2c_get_clientdata(client);
0464     struct imx214 *imx214 = to_imx214(sd);
0465 
0466     gpiod_set_value_cansleep(imx214->enable_gpio, 0);
0467 
0468     clk_disable_unprepare(imx214->xclk);
0469 
0470     regulator_bulk_disable(IMX214_NUM_SUPPLIES, imx214->supplies);
0471     usleep_range(10, 20);
0472 
0473     return 0;
0474 }
0475 
0476 static int imx214_enum_mbus_code(struct v4l2_subdev *sd,
0477                  struct v4l2_subdev_state *sd_state,
0478                  struct v4l2_subdev_mbus_code_enum *code)
0479 {
0480     if (code->index > 0)
0481         return -EINVAL;
0482 
0483     code->code = IMX214_MBUS_CODE;
0484 
0485     return 0;
0486 }
0487 
0488 static int imx214_enum_frame_size(struct v4l2_subdev *subdev,
0489                   struct v4l2_subdev_state *sd_state,
0490                   struct v4l2_subdev_frame_size_enum *fse)
0491 {
0492     if (fse->code != IMX214_MBUS_CODE)
0493         return -EINVAL;
0494 
0495     if (fse->index >= ARRAY_SIZE(imx214_modes))
0496         return -EINVAL;
0497 
0498     fse->min_width = fse->max_width = imx214_modes[fse->index].width;
0499     fse->min_height = fse->max_height = imx214_modes[fse->index].height;
0500 
0501     return 0;
0502 }
0503 
0504 #ifdef CONFIG_VIDEO_ADV_DEBUG
0505 static int imx214_s_register(struct v4l2_subdev *subdev,
0506                  const struct v4l2_dbg_register *reg)
0507 {
0508     struct imx214 *imx214 = container_of(subdev, struct imx214, sd);
0509 
0510     return regmap_write(imx214->regmap, reg->reg, reg->val);
0511 }
0512 
0513 static int imx214_g_register(struct v4l2_subdev *subdev,
0514                  struct v4l2_dbg_register *reg)
0515 {
0516     struct imx214 *imx214 = container_of(subdev, struct imx214, sd);
0517     unsigned int aux;
0518     int ret;
0519 
0520     reg->size = 1;
0521     ret = regmap_read(imx214->regmap, reg->reg, &aux);
0522     reg->val = aux;
0523 
0524     return ret;
0525 }
0526 #endif
0527 
0528 static const struct v4l2_subdev_core_ops imx214_core_ops = {
0529 #ifdef CONFIG_VIDEO_ADV_DEBUG
0530     .g_register = imx214_g_register,
0531     .s_register = imx214_s_register,
0532 #endif
0533 };
0534 
0535 static struct v4l2_mbus_framefmt *
0536 __imx214_get_pad_format(struct imx214 *imx214,
0537             struct v4l2_subdev_state *sd_state,
0538             unsigned int pad,
0539             enum v4l2_subdev_format_whence which)
0540 {
0541     switch (which) {
0542     case V4L2_SUBDEV_FORMAT_TRY:
0543         return v4l2_subdev_get_try_format(&imx214->sd, sd_state, pad);
0544     case V4L2_SUBDEV_FORMAT_ACTIVE:
0545         return &imx214->fmt;
0546     default:
0547         return NULL;
0548     }
0549 }
0550 
0551 static int imx214_get_format(struct v4l2_subdev *sd,
0552                  struct v4l2_subdev_state *sd_state,
0553                  struct v4l2_subdev_format *format)
0554 {
0555     struct imx214 *imx214 = to_imx214(sd);
0556 
0557     mutex_lock(&imx214->mutex);
0558     format->format = *__imx214_get_pad_format(imx214, sd_state,
0559                           format->pad,
0560                           format->which);
0561     mutex_unlock(&imx214->mutex);
0562 
0563     return 0;
0564 }
0565 
0566 static struct v4l2_rect *
0567 __imx214_get_pad_crop(struct imx214 *imx214,
0568               struct v4l2_subdev_state *sd_state,
0569               unsigned int pad, enum v4l2_subdev_format_whence which)
0570 {
0571     switch (which) {
0572     case V4L2_SUBDEV_FORMAT_TRY:
0573         return v4l2_subdev_get_try_crop(&imx214->sd, sd_state, pad);
0574     case V4L2_SUBDEV_FORMAT_ACTIVE:
0575         return &imx214->crop;
0576     default:
0577         return NULL;
0578     }
0579 }
0580 
0581 static int imx214_set_format(struct v4l2_subdev *sd,
0582                  struct v4l2_subdev_state *sd_state,
0583                  struct v4l2_subdev_format *format)
0584 {
0585     struct imx214 *imx214 = to_imx214(sd);
0586     struct v4l2_mbus_framefmt *__format;
0587     struct v4l2_rect *__crop;
0588     const struct imx214_mode *mode;
0589 
0590     mutex_lock(&imx214->mutex);
0591 
0592     __crop = __imx214_get_pad_crop(imx214, sd_state, format->pad,
0593                        format->which);
0594 
0595     mode = v4l2_find_nearest_size(imx214_modes,
0596                       ARRAY_SIZE(imx214_modes), width, height,
0597                       format->format.width,
0598                       format->format.height);
0599 
0600     __crop->width = mode->width;
0601     __crop->height = mode->height;
0602 
0603     __format = __imx214_get_pad_format(imx214, sd_state, format->pad,
0604                        format->which);
0605     __format->width = __crop->width;
0606     __format->height = __crop->height;
0607     __format->code = IMX214_MBUS_CODE;
0608     __format->field = V4L2_FIELD_NONE;
0609     __format->colorspace = V4L2_COLORSPACE_SRGB;
0610     __format->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(__format->colorspace);
0611     __format->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
0612                 __format->colorspace, __format->ycbcr_enc);
0613     __format->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(__format->colorspace);
0614 
0615     format->format = *__format;
0616 
0617     mutex_unlock(&imx214->mutex);
0618 
0619     return 0;
0620 }
0621 
0622 static int imx214_get_selection(struct v4l2_subdev *sd,
0623                 struct v4l2_subdev_state *sd_state,
0624                 struct v4l2_subdev_selection *sel)
0625 {
0626     struct imx214 *imx214 = to_imx214(sd);
0627 
0628     if (sel->target != V4L2_SEL_TGT_CROP)
0629         return -EINVAL;
0630 
0631     mutex_lock(&imx214->mutex);
0632     sel->r = *__imx214_get_pad_crop(imx214, sd_state, sel->pad,
0633                     sel->which);
0634     mutex_unlock(&imx214->mutex);
0635     return 0;
0636 }
0637 
0638 static int imx214_entity_init_cfg(struct v4l2_subdev *subdev,
0639                   struct v4l2_subdev_state *sd_state)
0640 {
0641     struct v4l2_subdev_format fmt = { };
0642 
0643     fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
0644     fmt.format.width = imx214_modes[0].width;
0645     fmt.format.height = imx214_modes[0].height;
0646 
0647     imx214_set_format(subdev, sd_state, &fmt);
0648 
0649     return 0;
0650 }
0651 
0652 static int imx214_set_ctrl(struct v4l2_ctrl *ctrl)
0653 {
0654     struct imx214 *imx214 = container_of(ctrl->handler,
0655                          struct imx214, ctrls);
0656     u8 vals[2];
0657     int ret;
0658 
0659     /*
0660      * Applying V4L2 control value only happens
0661      * when power is up for streaming
0662      */
0663     if (!pm_runtime_get_if_in_use(imx214->dev))
0664         return 0;
0665 
0666     switch (ctrl->id) {
0667     case V4L2_CID_EXPOSURE:
0668         vals[1] = ctrl->val;
0669         vals[0] = ctrl->val >> 8;
0670         ret = regmap_bulk_write(imx214->regmap, 0x202, vals, 2);
0671         if (ret < 0)
0672             dev_err(imx214->dev, "Error %d\n", ret);
0673         ret = 0;
0674         break;
0675 
0676     default:
0677         ret = -EINVAL;
0678     }
0679 
0680     pm_runtime_put(imx214->dev);
0681 
0682     return ret;
0683 }
0684 
0685 static const struct v4l2_ctrl_ops imx214_ctrl_ops = {
0686     .s_ctrl = imx214_set_ctrl,
0687 };
0688 
0689 #define MAX_CMD 4
0690 static int imx214_write_table(struct imx214 *imx214,
0691                   const struct reg_8 table[])
0692 {
0693     u8 vals[MAX_CMD];
0694     int i;
0695     int ret;
0696 
0697     for (; table->addr != IMX214_TABLE_END ; table++) {
0698         if (table->addr == IMX214_TABLE_WAIT_MS) {
0699             usleep_range(table->val * 1000,
0700                      table->val * 1000 + 500);
0701             continue;
0702         }
0703 
0704         for (i = 0; i < MAX_CMD; i++) {
0705             if (table[i].addr != (table[0].addr + i))
0706                 break;
0707             vals[i] = table[i].val;
0708         }
0709 
0710         ret = regmap_bulk_write(imx214->regmap, table->addr, vals, i);
0711 
0712         if (ret) {
0713             dev_err(imx214->dev, "write_table error: %d\n", ret);
0714             return ret;
0715         }
0716 
0717         table += i - 1;
0718     }
0719 
0720     return 0;
0721 }
0722 
0723 static int imx214_start_streaming(struct imx214 *imx214)
0724 {
0725     const struct imx214_mode *mode;
0726     int ret;
0727 
0728     mutex_lock(&imx214->mutex);
0729     ret = imx214_write_table(imx214, mode_table_common);
0730     if (ret < 0) {
0731         dev_err(imx214->dev, "could not sent common table %d\n", ret);
0732         goto error;
0733     }
0734 
0735     mode = v4l2_find_nearest_size(imx214_modes,
0736                 ARRAY_SIZE(imx214_modes), width, height,
0737                 imx214->fmt.width, imx214->fmt.height);
0738     ret = imx214_write_table(imx214, mode->reg_table);
0739     if (ret < 0) {
0740         dev_err(imx214->dev, "could not sent mode table %d\n", ret);
0741         goto error;
0742     }
0743     ret = __v4l2_ctrl_handler_setup(&imx214->ctrls);
0744     if (ret < 0) {
0745         dev_err(imx214->dev, "could not sync v4l2 controls\n");
0746         goto error;
0747     }
0748     ret = regmap_write(imx214->regmap, 0x100, 1);
0749     if (ret < 0) {
0750         dev_err(imx214->dev, "could not sent start table %d\n", ret);
0751         goto error;
0752     }
0753 
0754     mutex_unlock(&imx214->mutex);
0755     return 0;
0756 
0757 error:
0758     mutex_unlock(&imx214->mutex);
0759     return ret;
0760 }
0761 
0762 static int imx214_stop_streaming(struct imx214 *imx214)
0763 {
0764     int ret;
0765 
0766     ret = regmap_write(imx214->regmap, 0x100, 0);
0767     if (ret < 0)
0768         dev_err(imx214->dev, "could not sent stop table %d\n",  ret);
0769 
0770     return ret;
0771 }
0772 
0773 static int imx214_s_stream(struct v4l2_subdev *subdev, int enable)
0774 {
0775     struct imx214 *imx214 = to_imx214(subdev);
0776     int ret;
0777 
0778     if (imx214->streaming == enable)
0779         return 0;
0780 
0781     if (enable) {
0782         ret = pm_runtime_resume_and_get(imx214->dev);
0783         if (ret < 0)
0784             return ret;
0785 
0786         ret = imx214_start_streaming(imx214);
0787         if (ret < 0)
0788             goto err_rpm_put;
0789     } else {
0790         ret = imx214_stop_streaming(imx214);
0791         if (ret < 0)
0792             goto err_rpm_put;
0793         pm_runtime_put(imx214->dev);
0794     }
0795 
0796     imx214->streaming = enable;
0797     return 0;
0798 
0799 err_rpm_put:
0800     pm_runtime_put(imx214->dev);
0801     return ret;
0802 }
0803 
0804 static int imx214_g_frame_interval(struct v4l2_subdev *subdev,
0805                    struct v4l2_subdev_frame_interval *fival)
0806 {
0807     fival->interval.numerator = 1;
0808     fival->interval.denominator = IMX214_FPS;
0809 
0810     return 0;
0811 }
0812 
0813 static int imx214_enum_frame_interval(struct v4l2_subdev *subdev,
0814                 struct v4l2_subdev_state *sd_state,
0815                 struct v4l2_subdev_frame_interval_enum *fie)
0816 {
0817     const struct imx214_mode *mode;
0818 
0819     if (fie->index != 0)
0820         return -EINVAL;
0821 
0822     mode = v4l2_find_nearest_size(imx214_modes,
0823                 ARRAY_SIZE(imx214_modes), width, height,
0824                 fie->width, fie->height);
0825 
0826     fie->code = IMX214_MBUS_CODE;
0827     fie->width = mode->width;
0828     fie->height = mode->height;
0829     fie->interval.numerator = 1;
0830     fie->interval.denominator = IMX214_FPS;
0831 
0832     return 0;
0833 }
0834 
0835 static const struct v4l2_subdev_video_ops imx214_video_ops = {
0836     .s_stream = imx214_s_stream,
0837     .g_frame_interval = imx214_g_frame_interval,
0838     .s_frame_interval = imx214_g_frame_interval,
0839 };
0840 
0841 static const struct v4l2_subdev_pad_ops imx214_subdev_pad_ops = {
0842     .enum_mbus_code = imx214_enum_mbus_code,
0843     .enum_frame_size = imx214_enum_frame_size,
0844     .enum_frame_interval = imx214_enum_frame_interval,
0845     .get_fmt = imx214_get_format,
0846     .set_fmt = imx214_set_format,
0847     .get_selection = imx214_get_selection,
0848     .init_cfg = imx214_entity_init_cfg,
0849 };
0850 
0851 static const struct v4l2_subdev_ops imx214_subdev_ops = {
0852     .core = &imx214_core_ops,
0853     .video = &imx214_video_ops,
0854     .pad = &imx214_subdev_pad_ops,
0855 };
0856 
0857 static const struct regmap_config sensor_regmap_config = {
0858     .reg_bits = 16,
0859     .val_bits = 8,
0860     .cache_type = REGCACHE_RBTREE,
0861 };
0862 
0863 static int imx214_get_regulators(struct device *dev, struct imx214 *imx214)
0864 {
0865     unsigned int i;
0866 
0867     for (i = 0; i < IMX214_NUM_SUPPLIES; i++)
0868         imx214->supplies[i].supply = imx214_supply_name[i];
0869 
0870     return devm_regulator_bulk_get(dev, IMX214_NUM_SUPPLIES,
0871                        imx214->supplies);
0872 }
0873 
0874 static int imx214_parse_fwnode(struct device *dev)
0875 {
0876     struct fwnode_handle *endpoint;
0877     struct v4l2_fwnode_endpoint bus_cfg = {
0878         .bus_type = V4L2_MBUS_CSI2_DPHY,
0879     };
0880     unsigned int i;
0881     int ret;
0882 
0883     endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
0884     if (!endpoint) {
0885         dev_err(dev, "endpoint node not found\n");
0886         return -EINVAL;
0887     }
0888 
0889     ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg);
0890     if (ret) {
0891         dev_err(dev, "parsing endpoint node failed\n");
0892         goto done;
0893     }
0894 
0895     for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
0896         if (bus_cfg.link_frequencies[i] == IMX214_DEFAULT_LINK_FREQ)
0897             break;
0898 
0899     if (i == bus_cfg.nr_of_link_frequencies) {
0900         dev_err(dev, "link-frequencies %d not supported, Please review your DT\n",
0901             IMX214_DEFAULT_LINK_FREQ);
0902         ret = -EINVAL;
0903         goto done;
0904     }
0905 
0906 done:
0907     v4l2_fwnode_endpoint_free(&bus_cfg);
0908     fwnode_handle_put(endpoint);
0909     return ret;
0910 }
0911 
0912 static int __maybe_unused imx214_suspend(struct device *dev)
0913 {
0914     struct i2c_client *client = to_i2c_client(dev);
0915     struct v4l2_subdev *sd = i2c_get_clientdata(client);
0916     struct imx214 *imx214 = to_imx214(sd);
0917 
0918     if (imx214->streaming)
0919         imx214_stop_streaming(imx214);
0920 
0921     return 0;
0922 }
0923 
0924 static int __maybe_unused imx214_resume(struct device *dev)
0925 {
0926     struct i2c_client *client = to_i2c_client(dev);
0927     struct v4l2_subdev *sd = i2c_get_clientdata(client);
0928     struct imx214 *imx214 = to_imx214(sd);
0929     int ret;
0930 
0931     if (imx214->streaming) {
0932         ret = imx214_start_streaming(imx214);
0933         if (ret)
0934             goto error;
0935     }
0936 
0937     return 0;
0938 
0939 error:
0940     imx214_stop_streaming(imx214);
0941     imx214->streaming = 0;
0942     return ret;
0943 }
0944 
0945 static int imx214_probe(struct i2c_client *client)
0946 {
0947     struct device *dev = &client->dev;
0948     struct imx214 *imx214;
0949     static const s64 link_freq[] = {
0950         IMX214_DEFAULT_LINK_FREQ,
0951     };
0952     static const struct v4l2_area unit_size = {
0953         .width = 1120,
0954         .height = 1120,
0955     };
0956     int ret;
0957 
0958     ret = imx214_parse_fwnode(dev);
0959     if (ret)
0960         return ret;
0961 
0962     imx214 = devm_kzalloc(dev, sizeof(*imx214), GFP_KERNEL);
0963     if (!imx214)
0964         return -ENOMEM;
0965 
0966     imx214->dev = dev;
0967 
0968     imx214->xclk = devm_clk_get(dev, NULL);
0969     if (IS_ERR(imx214->xclk)) {
0970         dev_err(dev, "could not get xclk");
0971         return PTR_ERR(imx214->xclk);
0972     }
0973 
0974     ret = clk_set_rate(imx214->xclk, IMX214_DEFAULT_CLK_FREQ);
0975     if (ret) {
0976         dev_err(dev, "could not set xclk frequency\n");
0977         return ret;
0978     }
0979 
0980     ret = imx214_get_regulators(dev, imx214);
0981     if (ret < 0) {
0982         dev_err(dev, "cannot get regulators\n");
0983         return ret;
0984     }
0985 
0986     imx214->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
0987     if (IS_ERR(imx214->enable_gpio)) {
0988         dev_err(dev, "cannot get enable gpio\n");
0989         return PTR_ERR(imx214->enable_gpio);
0990     }
0991 
0992     imx214->regmap = devm_regmap_init_i2c(client, &sensor_regmap_config);
0993     if (IS_ERR(imx214->regmap)) {
0994         dev_err(dev, "regmap init failed\n");
0995         return PTR_ERR(imx214->regmap);
0996     }
0997 
0998     v4l2_i2c_subdev_init(&imx214->sd, client, &imx214_subdev_ops);
0999 
1000     /*
1001      * Enable power initially, to avoid warnings
1002      * from clk_disable on power_off
1003      */
1004     imx214_power_on(imx214->dev);
1005 
1006     pm_runtime_set_active(imx214->dev);
1007     pm_runtime_enable(imx214->dev);
1008     pm_runtime_idle(imx214->dev);
1009 
1010     v4l2_ctrl_handler_init(&imx214->ctrls, 3);
1011 
1012     imx214->pixel_rate = v4l2_ctrl_new_std(&imx214->ctrls, NULL,
1013                            V4L2_CID_PIXEL_RATE, 0,
1014                            IMX214_DEFAULT_PIXEL_RATE, 1,
1015                            IMX214_DEFAULT_PIXEL_RATE);
1016     imx214->link_freq = v4l2_ctrl_new_int_menu(&imx214->ctrls, NULL,
1017                            V4L2_CID_LINK_FREQ,
1018                            ARRAY_SIZE(link_freq) - 1,
1019                            0, link_freq);
1020     if (imx214->link_freq)
1021         imx214->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1022 
1023     /*
1024      * WARNING!
1025      * Values obtained reverse engineering blobs and/or devices.
1026      * Ranges and functionality might be wrong.
1027      *
1028      * Sony, please release some register set documentation for the
1029      * device.
1030      *
1031      * Yours sincerely, Ricardo.
1032      */
1033     imx214->exposure = v4l2_ctrl_new_std(&imx214->ctrls, &imx214_ctrl_ops,
1034                          V4L2_CID_EXPOSURE,
1035                          0, 3184, 1, 0x0c70);
1036 
1037     imx214->unit_size = v4l2_ctrl_new_std_compound(&imx214->ctrls,
1038                 NULL,
1039                 V4L2_CID_UNIT_CELL_SIZE,
1040                 v4l2_ctrl_ptr_create((void *)&unit_size));
1041     ret = imx214->ctrls.error;
1042     if (ret) {
1043         dev_err(&client->dev, "%s control init failed (%d)\n",
1044             __func__, ret);
1045         goto free_ctrl;
1046     }
1047 
1048     imx214->sd.ctrl_handler = &imx214->ctrls;
1049     mutex_init(&imx214->mutex);
1050     imx214->ctrls.lock = &imx214->mutex;
1051 
1052     imx214->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1053     imx214->pad.flags = MEDIA_PAD_FL_SOURCE;
1054     imx214->sd.dev = &client->dev;
1055     imx214->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1056 
1057     ret = media_entity_pads_init(&imx214->sd.entity, 1, &imx214->pad);
1058     if (ret < 0) {
1059         dev_err(dev, "could not register media entity\n");
1060         goto free_ctrl;
1061     }
1062 
1063     imx214_entity_init_cfg(&imx214->sd, NULL);
1064 
1065     ret = v4l2_async_register_subdev_sensor(&imx214->sd);
1066     if (ret < 0) {
1067         dev_err(dev, "could not register v4l2 device\n");
1068         goto free_entity;
1069     }
1070 
1071     return 0;
1072 
1073 free_entity:
1074     media_entity_cleanup(&imx214->sd.entity);
1075 free_ctrl:
1076     mutex_destroy(&imx214->mutex);
1077     v4l2_ctrl_handler_free(&imx214->ctrls);
1078     pm_runtime_disable(imx214->dev);
1079 
1080     return ret;
1081 }
1082 
1083 static int imx214_remove(struct i2c_client *client)
1084 {
1085     struct v4l2_subdev *sd = i2c_get_clientdata(client);
1086     struct imx214 *imx214 = to_imx214(sd);
1087 
1088     v4l2_async_unregister_subdev(&imx214->sd);
1089     media_entity_cleanup(&imx214->sd.entity);
1090     v4l2_ctrl_handler_free(&imx214->ctrls);
1091 
1092     pm_runtime_disable(&client->dev);
1093     pm_runtime_set_suspended(&client->dev);
1094 
1095     mutex_destroy(&imx214->mutex);
1096 
1097     return 0;
1098 }
1099 
1100 static const struct of_device_id imx214_of_match[] = {
1101     { .compatible = "sony,imx214" },
1102     { }
1103 };
1104 MODULE_DEVICE_TABLE(of, imx214_of_match);
1105 
1106 static const struct dev_pm_ops imx214_pm_ops = {
1107     SET_SYSTEM_SLEEP_PM_OPS(imx214_suspend, imx214_resume)
1108     SET_RUNTIME_PM_OPS(imx214_power_off, imx214_power_on, NULL)
1109 };
1110 
1111 static struct i2c_driver imx214_i2c_driver = {
1112     .driver = {
1113         .of_match_table = imx214_of_match,
1114         .pm = &imx214_pm_ops,
1115         .name  = "imx214",
1116     },
1117     .probe_new  = imx214_probe,
1118     .remove = imx214_remove,
1119 };
1120 
1121 module_i2c_driver(imx214_i2c_driver);
1122 
1123 MODULE_DESCRIPTION("Sony IMX214 Camera driver");
1124 MODULE_AUTHOR("Ricardo Ribalda <ribalda@kernel.org>");
1125 MODULE_LICENSE("GPL v2");