Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * et8ek8_driver.c
0004  *
0005  * Copyright (C) 2008 Nokia Corporation
0006  *
0007  * Contact: Sakari Ailus <sakari.ailus@iki.fi>
0008  *          Tuukka Toivonen <tuukkat76@gmail.com>
0009  *          Pavel Machek <pavel@ucw.cz>
0010  *
0011  * Based on code from Toni Leinonen <toni.leinonen@offcode.fi>.
0012  *
0013  * This driver is based on the Micron MT9T012 camera imager driver
0014  * (C) Texas Instruments.
0015  */
0016 
0017 #include <linux/clk.h>
0018 #include <linux/delay.h>
0019 #include <linux/gpio/consumer.h>
0020 #include <linux/i2c.h>
0021 #include <linux/kernel.h>
0022 #include <linux/module.h>
0023 #include <linux/mutex.h>
0024 #include <linux/regulator/consumer.h>
0025 #include <linux/slab.h>
0026 #include <linux/sort.h>
0027 #include <linux/v4l2-mediabus.h>
0028 
0029 #include <media/media-entity.h>
0030 #include <media/v4l2-ctrls.h>
0031 #include <media/v4l2-device.h>
0032 #include <media/v4l2-subdev.h>
0033 
0034 #include "et8ek8_reg.h"
0035 
0036 #define ET8EK8_NAME     "et8ek8"
0037 #define ET8EK8_PRIV_MEM_SIZE    128
0038 #define ET8EK8_MAX_MSG      8
0039 
0040 struct et8ek8_sensor {
0041     struct v4l2_subdev subdev;
0042     struct media_pad pad;
0043     struct v4l2_mbus_framefmt format;
0044     struct gpio_desc *reset;
0045     struct regulator *vana;
0046     struct clk *ext_clk;
0047     u32 xclk_freq;
0048 
0049     u16 version;
0050 
0051     struct v4l2_ctrl_handler ctrl_handler;
0052     struct v4l2_ctrl *exposure;
0053     struct v4l2_ctrl *pixel_rate;
0054     struct et8ek8_reglist *current_reglist;
0055 
0056     u8 priv_mem[ET8EK8_PRIV_MEM_SIZE];
0057 
0058     struct mutex power_lock;
0059     int power_count;
0060 };
0061 
0062 #define to_et8ek8_sensor(sd)    container_of(sd, struct et8ek8_sensor, subdev)
0063 
0064 enum et8ek8_versions {
0065     ET8EK8_REV_1 = 0x0001,
0066     ET8EK8_REV_2,
0067 };
0068 
0069 /*
0070  * This table describes what should be written to the sensor register
0071  * for each gain value. The gain(index in the table) is in terms of
0072  * 0.1EV, i.e. 10 indexes in the table give 2 time more gain [0] in
0073  * the *analog gain, [1] in the digital gain
0074  *
0075  * Analog gain [dB] = 20*log10(regvalue/32); 0x20..0x100
0076  */
0077 static struct et8ek8_gain {
0078     u16 analog;
0079     u16 digital;
0080 } const et8ek8_gain_table[] = {
0081     { 32,    0},  /* x1 */
0082     { 34,    0},
0083     { 37,    0},
0084     { 39,    0},
0085     { 42,    0},
0086     { 45,    0},
0087     { 49,    0},
0088     { 52,    0},
0089     { 56,    0},
0090     { 60,    0},
0091     { 64,    0},  /* x2 */
0092     { 69,    0},
0093     { 74,    0},
0094     { 79,    0},
0095     { 84,    0},
0096     { 91,    0},
0097     { 97,    0},
0098     {104,    0},
0099     {111,    0},
0100     {119,    0},
0101     {128,    0},  /* x4 */
0102     {137,    0},
0103     {147,    0},
0104     {158,    0},
0105     {169,    0},
0106     {181,    0},
0107     {194,    0},
0108     {208,    0},
0109     {223,    0},
0110     {239,    0},
0111     {256,    0},  /* x8 */
0112     {256,   73},
0113     {256,  152},
0114     {256,  236},
0115     {256,  327},
0116     {256,  424},
0117     {256,  528},
0118     {256,  639},
0119     {256,  758},
0120     {256,  886},
0121     {256, 1023},  /* x16 */
0122 };
0123 
0124 /* Register definitions */
0125 #define REG_REVISION_NUMBER_L   0x1200
0126 #define REG_REVISION_NUMBER_H   0x1201
0127 
0128 #define PRIV_MEM_START_REG  0x0008
0129 #define PRIV_MEM_WIN_SIZE   8
0130 
0131 #define ET8EK8_I2C_DELAY    3   /* msec delay b/w accesses */
0132 
0133 #define USE_CRC         1
0134 
0135 /*
0136  * Register access helpers
0137  *
0138  * Read a 8/16/32-bit i2c register.  The value is returned in 'val'.
0139  * Returns zero if successful, or non-zero otherwise.
0140  */
0141 static int et8ek8_i2c_read_reg(struct i2c_client *client, u16 data_length,
0142                    u16 reg, u32 *val)
0143 {
0144     int r;
0145     struct i2c_msg msg;
0146     unsigned char data[4];
0147 
0148     if (!client->adapter)
0149         return -ENODEV;
0150     if (data_length != ET8EK8_REG_8BIT && data_length != ET8EK8_REG_16BIT)
0151         return -EINVAL;
0152 
0153     msg.addr = client->addr;
0154     msg.flags = 0;
0155     msg.len = 2;
0156     msg.buf = data;
0157 
0158     /* high byte goes out first */
0159     data[0] = (u8) (reg >> 8);
0160     data[1] = (u8) (reg & 0xff);
0161     r = i2c_transfer(client->adapter, &msg, 1);
0162     if (r < 0)
0163         goto err;
0164 
0165     msg.len = data_length;
0166     msg.flags = I2C_M_RD;
0167     r = i2c_transfer(client->adapter, &msg, 1);
0168     if (r < 0)
0169         goto err;
0170 
0171     *val = 0;
0172     /* high byte comes first */
0173     if (data_length == ET8EK8_REG_8BIT)
0174         *val = data[0];
0175     else
0176         *val = (data[1] << 8) + data[0];
0177 
0178     return 0;
0179 
0180 err:
0181     dev_err(&client->dev, "read from offset 0x%x error %d\n", reg, r);
0182 
0183     return r;
0184 }
0185 
0186 static void et8ek8_i2c_create_msg(struct i2c_client *client, u16 len, u16 reg,
0187                   u32 val, struct i2c_msg *msg,
0188                   unsigned char *buf)
0189 {
0190     msg->addr = client->addr;
0191     msg->flags = 0; /* Write */
0192     msg->len = 2 + len;
0193     msg->buf = buf;
0194 
0195     /* high byte goes out first */
0196     buf[0] = (u8) (reg >> 8);
0197     buf[1] = (u8) (reg & 0xff);
0198 
0199     switch (len) {
0200     case ET8EK8_REG_8BIT:
0201         buf[2] = (u8) (val) & 0xff;
0202         break;
0203     case ET8EK8_REG_16BIT:
0204         buf[2] = (u8) (val) & 0xff;
0205         buf[3] = (u8) (val >> 8) & 0xff;
0206         break;
0207     default:
0208         WARN_ONCE(1, ET8EK8_NAME ": %s: invalid message length.\n",
0209               __func__);
0210     }
0211 }
0212 
0213 /*
0214  * A buffered write method that puts the wanted register write
0215  * commands in smaller number of message lists and passes the lists to
0216  * the i2c framework
0217  */
0218 static int et8ek8_i2c_buffered_write_regs(struct i2c_client *client,
0219                       const struct et8ek8_reg *wnext,
0220                       int cnt)
0221 {
0222     struct i2c_msg msg[ET8EK8_MAX_MSG];
0223     unsigned char data[ET8EK8_MAX_MSG][6];
0224     int wcnt = 0;
0225     u16 reg, data_length;
0226     u32 val;
0227     int rval;
0228 
0229     /* Create new write messages for all writes */
0230     while (wcnt < cnt) {
0231         data_length = wnext->type;
0232         reg = wnext->reg;
0233         val = wnext->val;
0234         wnext++;
0235 
0236         et8ek8_i2c_create_msg(client, data_length, reg,
0237                     val, &msg[wcnt], &data[wcnt][0]);
0238 
0239         /* Update write count */
0240         wcnt++;
0241 
0242         if (wcnt < ET8EK8_MAX_MSG)
0243             continue;
0244 
0245         rval = i2c_transfer(client->adapter, msg, wcnt);
0246         if (rval < 0)
0247             return rval;
0248 
0249         cnt -= wcnt;
0250         wcnt = 0;
0251     }
0252 
0253     rval = i2c_transfer(client->adapter, msg, wcnt);
0254 
0255     return rval < 0 ? rval : 0;
0256 }
0257 
0258 /*
0259  * Write a list of registers to i2c device.
0260  *
0261  * The list of registers is terminated by ET8EK8_REG_TERM.
0262  * Returns zero if successful, or non-zero otherwise.
0263  */
0264 static int et8ek8_i2c_write_regs(struct i2c_client *client,
0265                  const struct et8ek8_reg *regs)
0266 {
0267     int r, cnt = 0;
0268     const struct et8ek8_reg *next;
0269 
0270     if (!client->adapter)
0271         return -ENODEV;
0272 
0273     if (!regs)
0274         return -EINVAL;
0275 
0276     /* Initialize list pointers to the start of the list */
0277     next = regs;
0278 
0279     do {
0280         /*
0281          * We have to go through the list to figure out how
0282          * many regular writes we have in a row
0283          */
0284         while (next->type != ET8EK8_REG_TERM &&
0285                next->type != ET8EK8_REG_DELAY) {
0286             /*
0287              * Here we check that the actual length fields
0288              * are valid
0289              */
0290             if (WARN(next->type != ET8EK8_REG_8BIT &&
0291                  next->type != ET8EK8_REG_16BIT,
0292                  "Invalid type = %d", next->type)) {
0293                 return -EINVAL;
0294             }
0295             /*
0296              * Increment count of successive writes and
0297              * read pointer
0298              */
0299             cnt++;
0300             next++;
0301         }
0302 
0303         /* Now we start writing ... */
0304         r = et8ek8_i2c_buffered_write_regs(client, regs, cnt);
0305 
0306         /* ... and then check that everything was OK */
0307         if (r < 0) {
0308             dev_err(&client->dev, "i2c transfer error!\n");
0309             return r;
0310         }
0311 
0312         /*
0313          * If we ran into a sleep statement when going through
0314          * the list, this is where we snooze for the required time
0315          */
0316         if (next->type == ET8EK8_REG_DELAY) {
0317             msleep(next->val);
0318             /*
0319              * ZZZ ...
0320              * Update list pointers and cnt and start over ...
0321              */
0322             next++;
0323             regs = next;
0324             cnt = 0;
0325         }
0326     } while (next->type != ET8EK8_REG_TERM);
0327 
0328     return 0;
0329 }
0330 
0331 /*
0332  * Write to a 8/16-bit register.
0333  * Returns zero if successful, or non-zero otherwise.
0334  */
0335 static int et8ek8_i2c_write_reg(struct i2c_client *client, u16 data_length,
0336                 u16 reg, u32 val)
0337 {
0338     int r;
0339     struct i2c_msg msg;
0340     unsigned char data[6];
0341 
0342     if (!client->adapter)
0343         return -ENODEV;
0344     if (data_length != ET8EK8_REG_8BIT && data_length != ET8EK8_REG_16BIT)
0345         return -EINVAL;
0346 
0347     et8ek8_i2c_create_msg(client, data_length, reg, val, &msg, data);
0348 
0349     r = i2c_transfer(client->adapter, &msg, 1);
0350     if (r < 0) {
0351         dev_err(&client->dev,
0352             "wrote 0x%x to offset 0x%x error %d\n", val, reg, r);
0353         return r;
0354     }
0355 
0356     return 0;
0357 }
0358 
0359 static struct et8ek8_reglist *et8ek8_reglist_find_type(
0360         struct et8ek8_meta_reglist *meta,
0361         u16 type)
0362 {
0363     struct et8ek8_reglist **next = &meta->reglist[0].ptr;
0364 
0365     while (*next) {
0366         if ((*next)->type == type)
0367             return *next;
0368 
0369         next++;
0370     }
0371 
0372     return NULL;
0373 }
0374 
0375 static int et8ek8_i2c_reglist_find_write(struct i2c_client *client,
0376                      struct et8ek8_meta_reglist *meta,
0377                      u16 type)
0378 {
0379     struct et8ek8_reglist *reglist;
0380 
0381     reglist = et8ek8_reglist_find_type(meta, type);
0382     if (!reglist)
0383         return -EINVAL;
0384 
0385     return et8ek8_i2c_write_regs(client, reglist->regs);
0386 }
0387 
0388 static struct et8ek8_reglist **et8ek8_reglist_first(
0389         struct et8ek8_meta_reglist *meta)
0390 {
0391     return &meta->reglist[0].ptr;
0392 }
0393 
0394 static void et8ek8_reglist_to_mbus(const struct et8ek8_reglist *reglist,
0395                    struct v4l2_mbus_framefmt *fmt)
0396 {
0397     fmt->width = reglist->mode.window_width;
0398     fmt->height = reglist->mode.window_height;
0399     fmt->code = reglist->mode.bus_format;
0400 }
0401 
0402 static struct et8ek8_reglist *et8ek8_reglist_find_mode_fmt(
0403         struct et8ek8_meta_reglist *meta,
0404         struct v4l2_mbus_framefmt *fmt)
0405 {
0406     struct et8ek8_reglist **list = et8ek8_reglist_first(meta);
0407     struct et8ek8_reglist *best_match = NULL;
0408     struct et8ek8_reglist *best_other = NULL;
0409     struct v4l2_mbus_framefmt format;
0410     unsigned int max_dist_match = (unsigned int)-1;
0411     unsigned int max_dist_other = (unsigned int)-1;
0412 
0413     /*
0414      * Find the mode with the closest image size. The distance between
0415      * image sizes is the size in pixels of the non-overlapping regions
0416      * between the requested size and the frame-specified size.
0417      *
0418      * Store both the closest mode that matches the requested format, and
0419      * the closest mode for all other formats. The best match is returned
0420      * if found, otherwise the best mode with a non-matching format is
0421      * returned.
0422      */
0423     for (; *list; list++) {
0424         unsigned int dist;
0425 
0426         if ((*list)->type != ET8EK8_REGLIST_MODE)
0427             continue;
0428 
0429         et8ek8_reglist_to_mbus(*list, &format);
0430 
0431         dist = min(fmt->width, format.width)
0432              * min(fmt->height, format.height);
0433         dist = format.width * format.height
0434              + fmt->width * fmt->height - 2 * dist;
0435 
0436 
0437         if (fmt->code == format.code) {
0438             if (dist < max_dist_match || !best_match) {
0439                 best_match = *list;
0440                 max_dist_match = dist;
0441             }
0442         } else {
0443             if (dist < max_dist_other || !best_other) {
0444                 best_other = *list;
0445                 max_dist_other = dist;
0446             }
0447         }
0448     }
0449 
0450     return best_match ? best_match : best_other;
0451 }
0452 
0453 #define TIMEPERFRAME_AVG_FPS(t)                     \
0454     (((t).denominator + ((t).numerator >> 1)) / (t).numerator)
0455 
0456 static struct et8ek8_reglist *et8ek8_reglist_find_mode_ival(
0457         struct et8ek8_meta_reglist *meta,
0458         struct et8ek8_reglist *current_reglist,
0459         struct v4l2_fract *timeperframe)
0460 {
0461     int fps = TIMEPERFRAME_AVG_FPS(*timeperframe);
0462     struct et8ek8_reglist **list = et8ek8_reglist_first(meta);
0463     struct et8ek8_mode *current_mode = &current_reglist->mode;
0464 
0465     for (; *list; list++) {
0466         struct et8ek8_mode *mode = &(*list)->mode;
0467 
0468         if ((*list)->type != ET8EK8_REGLIST_MODE)
0469             continue;
0470 
0471         if (mode->window_width != current_mode->window_width ||
0472             mode->window_height != current_mode->window_height)
0473             continue;
0474 
0475         if (TIMEPERFRAME_AVG_FPS(mode->timeperframe) == fps)
0476             return *list;
0477     }
0478 
0479     return NULL;
0480 }
0481 
0482 static int et8ek8_reglist_cmp(const void *a, const void *b)
0483 {
0484     const struct et8ek8_reglist **list1 = (const struct et8ek8_reglist **)a,
0485         **list2 = (const struct et8ek8_reglist **)b;
0486 
0487     /* Put real modes in the beginning. */
0488     if ((*list1)->type == ET8EK8_REGLIST_MODE &&
0489         (*list2)->type != ET8EK8_REGLIST_MODE)
0490         return -1;
0491     if ((*list1)->type != ET8EK8_REGLIST_MODE &&
0492         (*list2)->type == ET8EK8_REGLIST_MODE)
0493         return 1;
0494 
0495     /* Descending width. */
0496     if ((*list1)->mode.window_width > (*list2)->mode.window_width)
0497         return -1;
0498     if ((*list1)->mode.window_width < (*list2)->mode.window_width)
0499         return 1;
0500 
0501     if ((*list1)->mode.window_height > (*list2)->mode.window_height)
0502         return -1;
0503     if ((*list1)->mode.window_height < (*list2)->mode.window_height)
0504         return 1;
0505 
0506     return 0;
0507 }
0508 
0509 static int et8ek8_reglist_import(struct i2c_client *client,
0510                  struct et8ek8_meta_reglist *meta)
0511 {
0512     int nlists = 0, i;
0513 
0514     dev_info(&client->dev, "meta_reglist version %s\n", meta->version);
0515 
0516     while (meta->reglist[nlists].ptr)
0517         nlists++;
0518 
0519     if (!nlists)
0520         return -EINVAL;
0521 
0522     sort(&meta->reglist[0].ptr, nlists, sizeof(meta->reglist[0].ptr),
0523          et8ek8_reglist_cmp, NULL);
0524 
0525     i = nlists;
0526     nlists = 0;
0527 
0528     while (i--) {
0529         struct et8ek8_reglist *list;
0530 
0531         list = meta->reglist[nlists].ptr;
0532 
0533         dev_dbg(&client->dev,
0534                "%s: type %d\tw %d\th %d\tfmt %x\tival %d/%d\tptr %p\n",
0535                __func__,
0536                list->type,
0537                list->mode.window_width, list->mode.window_height,
0538                list->mode.bus_format,
0539                list->mode.timeperframe.numerator,
0540                list->mode.timeperframe.denominator,
0541                (void *)meta->reglist[nlists].ptr);
0542 
0543         nlists++;
0544     }
0545 
0546     return 0;
0547 }
0548 
0549 /* Called to change the V4L2 gain control value. This function
0550  * rounds and clamps the given value and updates the V4L2 control value.
0551  * If power is on, also updates the sensor analog and digital gains.
0552  * gain is in 0.1 EV (exposure value) units.
0553  */
0554 static int et8ek8_set_gain(struct et8ek8_sensor *sensor, s32 gain)
0555 {
0556     struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
0557     struct et8ek8_gain new;
0558     int r;
0559 
0560     new = et8ek8_gain_table[gain];
0561 
0562     /* FIXME: optimise I2C writes! */
0563     r = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT,
0564                 0x124a, new.analog >> 8);
0565     if (r)
0566         return r;
0567     r = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT,
0568                 0x1249, new.analog & 0xff);
0569     if (r)
0570         return r;
0571 
0572     r = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT,
0573                 0x124d, new.digital >> 8);
0574     if (r)
0575         return r;
0576     r = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT,
0577                 0x124c, new.digital & 0xff);
0578 
0579     return r;
0580 }
0581 
0582 static int et8ek8_set_test_pattern(struct et8ek8_sensor *sensor, s32 mode)
0583 {
0584     struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
0585     int cbh_mode, cbv_mode, tp_mode, din_sw, r1420, rval;
0586 
0587     /* Values for normal mode */
0588     cbh_mode = 0;
0589     cbv_mode = 0;
0590     tp_mode  = 0;
0591     din_sw   = 0x00;
0592     r1420    = 0xF0;
0593 
0594     if (mode) {
0595         /* Test pattern mode */
0596         if (mode < 5) {
0597             cbh_mode = 1;
0598             cbv_mode = 1;
0599             tp_mode  = mode + 3;
0600         } else {
0601             cbh_mode = 0;
0602             cbv_mode = 0;
0603             tp_mode  = mode - 4 + 3;
0604         }
0605 
0606         din_sw   = 0x01;
0607         r1420    = 0xE0;
0608     }
0609 
0610     rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x111B,
0611                     tp_mode << 4);
0612     if (rval)
0613         return rval;
0614 
0615     rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1121,
0616                     cbh_mode << 7);
0617     if (rval)
0618         return rval;
0619 
0620     rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1124,
0621                     cbv_mode << 7);
0622     if (rval)
0623         return rval;
0624 
0625     rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x112C, din_sw);
0626     if (rval)
0627         return rval;
0628 
0629     return et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1420, r1420);
0630 }
0631 
0632 /* -----------------------------------------------------------------------------
0633  * V4L2 controls
0634  */
0635 
0636 static int et8ek8_set_ctrl(struct v4l2_ctrl *ctrl)
0637 {
0638     struct et8ek8_sensor *sensor =
0639         container_of(ctrl->handler, struct et8ek8_sensor, ctrl_handler);
0640 
0641     switch (ctrl->id) {
0642     case V4L2_CID_GAIN:
0643         return et8ek8_set_gain(sensor, ctrl->val);
0644 
0645     case V4L2_CID_EXPOSURE:
0646     {
0647         struct i2c_client *client =
0648             v4l2_get_subdevdata(&sensor->subdev);
0649 
0650         return et8ek8_i2c_write_reg(client, ET8EK8_REG_16BIT, 0x1243,
0651                         ctrl->val);
0652     }
0653 
0654     case V4L2_CID_TEST_PATTERN:
0655         return et8ek8_set_test_pattern(sensor, ctrl->val);
0656 
0657     case V4L2_CID_PIXEL_RATE:
0658         return 0;
0659 
0660     default:
0661         return -EINVAL;
0662     }
0663 }
0664 
0665 static const struct v4l2_ctrl_ops et8ek8_ctrl_ops = {
0666     .s_ctrl = et8ek8_set_ctrl,
0667 };
0668 
0669 static const char * const et8ek8_test_pattern_menu[] = {
0670     "Normal",
0671     "Vertical colorbar",
0672     "Horizontal colorbar",
0673     "Scale",
0674     "Ramp",
0675     "Small vertical colorbar",
0676     "Small horizontal colorbar",
0677     "Small scale",
0678     "Small ramp",
0679 };
0680 
0681 static int et8ek8_init_controls(struct et8ek8_sensor *sensor)
0682 {
0683     s32 max_rows;
0684 
0685     v4l2_ctrl_handler_init(&sensor->ctrl_handler, 4);
0686 
0687     /* V4L2_CID_GAIN */
0688     v4l2_ctrl_new_std(&sensor->ctrl_handler, &et8ek8_ctrl_ops,
0689               V4L2_CID_GAIN, 0, ARRAY_SIZE(et8ek8_gain_table) - 1,
0690               1, 0);
0691 
0692     max_rows = sensor->current_reglist->mode.max_exp;
0693     {
0694         u32 min = 1, max = max_rows;
0695 
0696         sensor->exposure =
0697             v4l2_ctrl_new_std(&sensor->ctrl_handler,
0698                       &et8ek8_ctrl_ops, V4L2_CID_EXPOSURE,
0699                       min, max, min, max);
0700     }
0701 
0702     /* V4L2_CID_PIXEL_RATE */
0703     sensor->pixel_rate =
0704         v4l2_ctrl_new_std(&sensor->ctrl_handler, &et8ek8_ctrl_ops,
0705         V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
0706 
0707     /* V4L2_CID_TEST_PATTERN */
0708     v4l2_ctrl_new_std_menu_items(&sensor->ctrl_handler,
0709                      &et8ek8_ctrl_ops, V4L2_CID_TEST_PATTERN,
0710                      ARRAY_SIZE(et8ek8_test_pattern_menu) - 1,
0711                      0, 0, et8ek8_test_pattern_menu);
0712 
0713     if (sensor->ctrl_handler.error)
0714         return sensor->ctrl_handler.error;
0715 
0716     sensor->subdev.ctrl_handler = &sensor->ctrl_handler;
0717 
0718     return 0;
0719 }
0720 
0721 static void et8ek8_update_controls(struct et8ek8_sensor *sensor)
0722 {
0723     struct v4l2_ctrl *ctrl;
0724     struct et8ek8_mode *mode = &sensor->current_reglist->mode;
0725 
0726     u32 min, max, pixel_rate;
0727     static const int S = 8;
0728 
0729     ctrl = sensor->exposure;
0730 
0731     min = 1;
0732     max = mode->max_exp;
0733 
0734     /*
0735      * Calculate average pixel clock per line. Assume buffers can spread
0736      * the data over horizontal blanking time. Rounding upwards.
0737      * Formula taken from stock Nokia N900 kernel.
0738      */
0739     pixel_rate = ((mode->pixel_clock + (1 << S) - 1) >> S) + mode->width;
0740     pixel_rate = mode->window_width * (pixel_rate - 1) / mode->width;
0741 
0742     __v4l2_ctrl_modify_range(ctrl, min, max, min, max);
0743     __v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate, pixel_rate << S);
0744 }
0745 
0746 static int et8ek8_configure(struct et8ek8_sensor *sensor)
0747 {
0748     struct v4l2_subdev *subdev = &sensor->subdev;
0749     struct i2c_client *client = v4l2_get_subdevdata(subdev);
0750     int rval;
0751 
0752     rval = et8ek8_i2c_write_regs(client, sensor->current_reglist->regs);
0753     if (rval)
0754         goto fail;
0755 
0756     /* Controls set while the power to the sensor is turned off are saved
0757      * but not applied to the hardware. Now that we're about to start
0758      * streaming apply all the current values to the hardware.
0759      */
0760     rval = v4l2_ctrl_handler_setup(&sensor->ctrl_handler);
0761     if (rval)
0762         goto fail;
0763 
0764     return 0;
0765 
0766 fail:
0767     dev_err(&client->dev, "sensor configuration failed\n");
0768 
0769     return rval;
0770 }
0771 
0772 static int et8ek8_stream_on(struct et8ek8_sensor *sensor)
0773 {
0774     struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
0775 
0776     return et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1252, 0xb0);
0777 }
0778 
0779 static int et8ek8_stream_off(struct et8ek8_sensor *sensor)
0780 {
0781     struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
0782 
0783     return et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1252, 0x30);
0784 }
0785 
0786 static int et8ek8_s_stream(struct v4l2_subdev *subdev, int streaming)
0787 {
0788     struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
0789     int ret;
0790 
0791     if (!streaming)
0792         return et8ek8_stream_off(sensor);
0793 
0794     ret = et8ek8_configure(sensor);
0795     if (ret < 0)
0796         return ret;
0797 
0798     return et8ek8_stream_on(sensor);
0799 }
0800 
0801 /* --------------------------------------------------------------------------
0802  * V4L2 subdev operations
0803  */
0804 
0805 static int et8ek8_power_off(struct et8ek8_sensor *sensor)
0806 {
0807     gpiod_set_value(sensor->reset, 0);
0808     udelay(1);
0809 
0810     clk_disable_unprepare(sensor->ext_clk);
0811 
0812     return regulator_disable(sensor->vana);
0813 }
0814 
0815 static int et8ek8_power_on(struct et8ek8_sensor *sensor)
0816 {
0817     struct v4l2_subdev *subdev = &sensor->subdev;
0818     struct i2c_client *client = v4l2_get_subdevdata(subdev);
0819     unsigned int xclk_freq;
0820     int val, rval;
0821 
0822     rval = regulator_enable(sensor->vana);
0823     if (rval) {
0824         dev_err(&client->dev, "failed to enable vana regulator\n");
0825         return rval;
0826     }
0827 
0828     if (sensor->current_reglist)
0829         xclk_freq = sensor->current_reglist->mode.ext_clock;
0830     else
0831         xclk_freq = sensor->xclk_freq;
0832 
0833     rval = clk_set_rate(sensor->ext_clk, xclk_freq);
0834     if (rval < 0) {
0835         dev_err(&client->dev, "unable to set extclk clock freq to %u\n",
0836             xclk_freq);
0837         goto out;
0838     }
0839     rval = clk_prepare_enable(sensor->ext_clk);
0840     if (rval < 0) {
0841         dev_err(&client->dev, "failed to enable extclk\n");
0842         goto out;
0843     }
0844 
0845     if (rval)
0846         goto out;
0847 
0848     udelay(10); /* I wish this is a good value */
0849 
0850     gpiod_set_value(sensor->reset, 1);
0851 
0852     msleep(5000 * 1000 / xclk_freq + 1); /* Wait 5000 cycles */
0853 
0854     rval = et8ek8_i2c_reglist_find_write(client, &meta_reglist,
0855                          ET8EK8_REGLIST_POWERON);
0856     if (rval)
0857         goto out;
0858 
0859 #ifdef USE_CRC
0860     rval = et8ek8_i2c_read_reg(client, ET8EK8_REG_8BIT, 0x1263, &val);
0861     if (rval)
0862         goto out;
0863 #if USE_CRC /* TODO get crc setting from DT */
0864     val |= BIT(4);
0865 #else
0866     val &= ~BIT(4);
0867 #endif
0868     rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1263, val);
0869     if (rval)
0870         goto out;
0871 #endif
0872 
0873 out:
0874     if (rval)
0875         et8ek8_power_off(sensor);
0876 
0877     return rval;
0878 }
0879 
0880 /* --------------------------------------------------------------------------
0881  * V4L2 subdev video operations
0882  */
0883 #define MAX_FMTS 4
0884 static int et8ek8_enum_mbus_code(struct v4l2_subdev *subdev,
0885                  struct v4l2_subdev_state *sd_state,
0886                  struct v4l2_subdev_mbus_code_enum *code)
0887 {
0888     struct et8ek8_reglist **list =
0889             et8ek8_reglist_first(&meta_reglist);
0890     u32 pixelformat[MAX_FMTS];
0891     int npixelformat = 0;
0892 
0893     if (code->index >= MAX_FMTS)
0894         return -EINVAL;
0895 
0896     for (; *list; list++) {
0897         struct et8ek8_mode *mode = &(*list)->mode;
0898         int i;
0899 
0900         if ((*list)->type != ET8EK8_REGLIST_MODE)
0901             continue;
0902 
0903         for (i = 0; i < npixelformat; i++) {
0904             if (pixelformat[i] == mode->bus_format)
0905                 break;
0906         }
0907         if (i != npixelformat)
0908             continue;
0909 
0910         if (code->index == npixelformat) {
0911             code->code = mode->bus_format;
0912             return 0;
0913         }
0914 
0915         pixelformat[npixelformat] = mode->bus_format;
0916         npixelformat++;
0917     }
0918 
0919     return -EINVAL;
0920 }
0921 
0922 static int et8ek8_enum_frame_size(struct v4l2_subdev *subdev,
0923                   struct v4l2_subdev_state *sd_state,
0924                   struct v4l2_subdev_frame_size_enum *fse)
0925 {
0926     struct et8ek8_reglist **list =
0927             et8ek8_reglist_first(&meta_reglist);
0928     struct v4l2_mbus_framefmt format;
0929     int cmp_width = INT_MAX;
0930     int cmp_height = INT_MAX;
0931     int index = fse->index;
0932 
0933     for (; *list; list++) {
0934         if ((*list)->type != ET8EK8_REGLIST_MODE)
0935             continue;
0936 
0937         et8ek8_reglist_to_mbus(*list, &format);
0938         if (fse->code != format.code)
0939             continue;
0940 
0941         /* Assume that the modes are grouped by frame size. */
0942         if (format.width == cmp_width && format.height == cmp_height)
0943             continue;
0944 
0945         cmp_width = format.width;
0946         cmp_height = format.height;
0947 
0948         if (index-- == 0) {
0949             fse->min_width = format.width;
0950             fse->min_height = format.height;
0951             fse->max_width = format.width;
0952             fse->max_height = format.height;
0953             return 0;
0954         }
0955     }
0956 
0957     return -EINVAL;
0958 }
0959 
0960 static int et8ek8_enum_frame_ival(struct v4l2_subdev *subdev,
0961                   struct v4l2_subdev_state *sd_state,
0962                   struct v4l2_subdev_frame_interval_enum *fie)
0963 {
0964     struct et8ek8_reglist **list =
0965             et8ek8_reglist_first(&meta_reglist);
0966     struct v4l2_mbus_framefmt format;
0967     int index = fie->index;
0968 
0969     for (; *list; list++) {
0970         struct et8ek8_mode *mode = &(*list)->mode;
0971 
0972         if ((*list)->type != ET8EK8_REGLIST_MODE)
0973             continue;
0974 
0975         et8ek8_reglist_to_mbus(*list, &format);
0976         if (fie->code != format.code)
0977             continue;
0978 
0979         if (fie->width != format.width || fie->height != format.height)
0980             continue;
0981 
0982         if (index-- == 0) {
0983             fie->interval = mode->timeperframe;
0984             return 0;
0985         }
0986     }
0987 
0988     return -EINVAL;
0989 }
0990 
0991 static struct v4l2_mbus_framefmt *
0992 __et8ek8_get_pad_format(struct et8ek8_sensor *sensor,
0993             struct v4l2_subdev_state *sd_state,
0994             unsigned int pad, enum v4l2_subdev_format_whence which)
0995 {
0996     switch (which) {
0997     case V4L2_SUBDEV_FORMAT_TRY:
0998         return v4l2_subdev_get_try_format(&sensor->subdev, sd_state,
0999                           pad);
1000     case V4L2_SUBDEV_FORMAT_ACTIVE:
1001         return &sensor->format;
1002     default:
1003         return NULL;
1004     }
1005 }
1006 
1007 static int et8ek8_get_pad_format(struct v4l2_subdev *subdev,
1008                  struct v4l2_subdev_state *sd_state,
1009                  struct v4l2_subdev_format *fmt)
1010 {
1011     struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1012     struct v4l2_mbus_framefmt *format;
1013 
1014     format = __et8ek8_get_pad_format(sensor, sd_state, fmt->pad,
1015                      fmt->which);
1016     if (!format)
1017         return -EINVAL;
1018 
1019     fmt->format = *format;
1020 
1021     return 0;
1022 }
1023 
1024 static int et8ek8_set_pad_format(struct v4l2_subdev *subdev,
1025                  struct v4l2_subdev_state *sd_state,
1026                  struct v4l2_subdev_format *fmt)
1027 {
1028     struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1029     struct v4l2_mbus_framefmt *format;
1030     struct et8ek8_reglist *reglist;
1031 
1032     format = __et8ek8_get_pad_format(sensor, sd_state, fmt->pad,
1033                      fmt->which);
1034     if (!format)
1035         return -EINVAL;
1036 
1037     reglist = et8ek8_reglist_find_mode_fmt(&meta_reglist, &fmt->format);
1038     et8ek8_reglist_to_mbus(reglist, &fmt->format);
1039     *format = fmt->format;
1040 
1041     if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1042         sensor->current_reglist = reglist;
1043         et8ek8_update_controls(sensor);
1044     }
1045 
1046     return 0;
1047 }
1048 
1049 static int et8ek8_get_frame_interval(struct v4l2_subdev *subdev,
1050                      struct v4l2_subdev_frame_interval *fi)
1051 {
1052     struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1053 
1054     memset(fi, 0, sizeof(*fi));
1055     fi->interval = sensor->current_reglist->mode.timeperframe;
1056 
1057     return 0;
1058 }
1059 
1060 static int et8ek8_set_frame_interval(struct v4l2_subdev *subdev,
1061                      struct v4l2_subdev_frame_interval *fi)
1062 {
1063     struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1064     struct et8ek8_reglist *reglist;
1065 
1066     reglist = et8ek8_reglist_find_mode_ival(&meta_reglist,
1067                         sensor->current_reglist,
1068                         &fi->interval);
1069 
1070     if (!reglist)
1071         return -EINVAL;
1072 
1073     if (sensor->current_reglist->mode.ext_clock != reglist->mode.ext_clock)
1074         return -EINVAL;
1075 
1076     sensor->current_reglist = reglist;
1077     et8ek8_update_controls(sensor);
1078 
1079     return 0;
1080 }
1081 
1082 static int et8ek8_g_priv_mem(struct v4l2_subdev *subdev)
1083 {
1084     struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1085     struct i2c_client *client = v4l2_get_subdevdata(subdev);
1086     unsigned int length = ET8EK8_PRIV_MEM_SIZE;
1087     unsigned int offset = 0;
1088     u8 *ptr  = sensor->priv_mem;
1089     int rval = 0;
1090 
1091     /* Read the EEPROM window-by-window, each window 8 bytes */
1092     do {
1093         u8 buffer[PRIV_MEM_WIN_SIZE];
1094         struct i2c_msg msg;
1095         int bytes, i;
1096         int ofs;
1097 
1098         /* Set the current window */
1099         rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x0001,
1100                         0xe0 | (offset >> 3));
1101         if (rval < 0)
1102             return rval;
1103 
1104         /* Wait for status bit */
1105         for (i = 0; i < 1000; ++i) {
1106             u32 status;
1107 
1108             rval = et8ek8_i2c_read_reg(client, ET8EK8_REG_8BIT,
1109                            0x0003, &status);
1110             if (rval < 0)
1111                 return rval;
1112             if (!(status & 0x08))
1113                 break;
1114             usleep_range(1000, 2000);
1115         }
1116 
1117         if (i == 1000)
1118             return -EIO;
1119 
1120         /* Read window, 8 bytes at once, and copy to user space */
1121         ofs = offset & 0x07;    /* Offset within this window */
1122         bytes = length + ofs > 8 ? 8-ofs : length;
1123         msg.addr = client->addr;
1124         msg.flags = 0;
1125         msg.len = 2;
1126         msg.buf = buffer;
1127         ofs += PRIV_MEM_START_REG;
1128         buffer[0] = (u8)(ofs >> 8);
1129         buffer[1] = (u8)(ofs & 0xFF);
1130 
1131         rval = i2c_transfer(client->adapter, &msg, 1);
1132         if (rval < 0)
1133             return rval;
1134 
1135         mdelay(ET8EK8_I2C_DELAY);
1136         msg.addr = client->addr;
1137         msg.len = bytes;
1138         msg.flags = I2C_M_RD;
1139         msg.buf = buffer;
1140         memset(buffer, 0, sizeof(buffer));
1141 
1142         rval = i2c_transfer(client->adapter, &msg, 1);
1143         if (rval < 0)
1144             return rval;
1145 
1146         rval = 0;
1147         memcpy(ptr, buffer, bytes);
1148 
1149         length -= bytes;
1150         offset += bytes;
1151         ptr += bytes;
1152     } while (length > 0);
1153 
1154     return rval;
1155 }
1156 
1157 static int et8ek8_dev_init(struct v4l2_subdev *subdev)
1158 {
1159     struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1160     struct i2c_client *client = v4l2_get_subdevdata(subdev);
1161     int rval, rev_l, rev_h;
1162 
1163     rval = et8ek8_power_on(sensor);
1164     if (rval) {
1165         dev_err(&client->dev, "could not power on\n");
1166         return rval;
1167     }
1168 
1169     rval = et8ek8_i2c_read_reg(client, ET8EK8_REG_8BIT,
1170                    REG_REVISION_NUMBER_L, &rev_l);
1171     if (!rval)
1172         rval = et8ek8_i2c_read_reg(client, ET8EK8_REG_8BIT,
1173                        REG_REVISION_NUMBER_H, &rev_h);
1174     if (rval) {
1175         dev_err(&client->dev, "no et8ek8 sensor detected\n");
1176         goto out_poweroff;
1177     }
1178 
1179     sensor->version = (rev_h << 8) + rev_l;
1180     if (sensor->version != ET8EK8_REV_1 && sensor->version != ET8EK8_REV_2)
1181         dev_info(&client->dev,
1182              "unknown version 0x%x detected, continuing anyway\n",
1183              sensor->version);
1184 
1185     rval = et8ek8_reglist_import(client, &meta_reglist);
1186     if (rval) {
1187         dev_err(&client->dev,
1188             "invalid register list %s, import failed\n",
1189             ET8EK8_NAME);
1190         goto out_poweroff;
1191     }
1192 
1193     sensor->current_reglist = et8ek8_reglist_find_type(&meta_reglist,
1194                                ET8EK8_REGLIST_MODE);
1195     if (!sensor->current_reglist) {
1196         dev_err(&client->dev,
1197             "invalid register list %s, no mode found\n",
1198             ET8EK8_NAME);
1199         rval = -ENODEV;
1200         goto out_poweroff;
1201     }
1202 
1203     et8ek8_reglist_to_mbus(sensor->current_reglist, &sensor->format);
1204 
1205     rval = et8ek8_i2c_reglist_find_write(client, &meta_reglist,
1206                          ET8EK8_REGLIST_POWERON);
1207     if (rval) {
1208         dev_err(&client->dev,
1209             "invalid register list %s, no POWERON mode found\n",
1210             ET8EK8_NAME);
1211         goto out_poweroff;
1212     }
1213     rval = et8ek8_stream_on(sensor); /* Needed to be able to read EEPROM */
1214     if (rval)
1215         goto out_poweroff;
1216     rval = et8ek8_g_priv_mem(subdev);
1217     if (rval)
1218         dev_warn(&client->dev,
1219             "can not read OTP (EEPROM) memory from sensor\n");
1220     rval = et8ek8_stream_off(sensor);
1221     if (rval)
1222         goto out_poweroff;
1223 
1224     rval = et8ek8_power_off(sensor);
1225     if (rval)
1226         goto out_poweroff;
1227 
1228     return 0;
1229 
1230 out_poweroff:
1231     et8ek8_power_off(sensor);
1232 
1233     return rval;
1234 }
1235 
1236 /* --------------------------------------------------------------------------
1237  * sysfs attributes
1238  */
1239 static ssize_t
1240 priv_mem_show(struct device *dev, struct device_attribute *attr, char *buf)
1241 {
1242     struct v4l2_subdev *subdev = dev_get_drvdata(dev);
1243     struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1244 
1245 #if PAGE_SIZE < ET8EK8_PRIV_MEM_SIZE
1246 #error PAGE_SIZE too small!
1247 #endif
1248 
1249     memcpy(buf, sensor->priv_mem, ET8EK8_PRIV_MEM_SIZE);
1250 
1251     return ET8EK8_PRIV_MEM_SIZE;
1252 }
1253 static DEVICE_ATTR_RO(priv_mem);
1254 
1255 /* --------------------------------------------------------------------------
1256  * V4L2 subdev core operations
1257  */
1258 
1259 static int
1260 et8ek8_registered(struct v4l2_subdev *subdev)
1261 {
1262     struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1263     struct i2c_client *client = v4l2_get_subdevdata(subdev);
1264     int rval;
1265 
1266     dev_dbg(&client->dev, "registered!");
1267 
1268     rval = device_create_file(&client->dev, &dev_attr_priv_mem);
1269     if (rval) {
1270         dev_err(&client->dev, "could not register sysfs entry\n");
1271         return rval;
1272     }
1273 
1274     rval = et8ek8_dev_init(subdev);
1275     if (rval)
1276         goto err_file;
1277 
1278     rval = et8ek8_init_controls(sensor);
1279     if (rval) {
1280         dev_err(&client->dev, "controls initialization failed\n");
1281         goto err_file;
1282     }
1283 
1284     __et8ek8_get_pad_format(sensor, NULL, 0, V4L2_SUBDEV_FORMAT_ACTIVE);
1285 
1286     return 0;
1287 
1288 err_file:
1289     device_remove_file(&client->dev, &dev_attr_priv_mem);
1290 
1291     return rval;
1292 }
1293 
1294 static int __et8ek8_set_power(struct et8ek8_sensor *sensor, bool on)
1295 {
1296     return on ? et8ek8_power_on(sensor) : et8ek8_power_off(sensor);
1297 }
1298 
1299 static int et8ek8_set_power(struct v4l2_subdev *subdev, int on)
1300 {
1301     struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1302     int ret = 0;
1303 
1304     mutex_lock(&sensor->power_lock);
1305 
1306     /* If the power count is modified from 0 to != 0 or from != 0 to 0,
1307      * update the power state.
1308      */
1309     if (sensor->power_count == !on) {
1310         ret = __et8ek8_set_power(sensor, !!on);
1311         if (ret < 0)
1312             goto done;
1313     }
1314 
1315     /* Update the power count. */
1316     sensor->power_count += on ? 1 : -1;
1317     WARN_ON(sensor->power_count < 0);
1318 
1319 done:
1320     mutex_unlock(&sensor->power_lock);
1321 
1322     return ret;
1323 }
1324 
1325 static int et8ek8_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1326 {
1327     struct et8ek8_sensor *sensor = to_et8ek8_sensor(sd);
1328     struct v4l2_mbus_framefmt *format;
1329     struct et8ek8_reglist *reglist;
1330 
1331     reglist = et8ek8_reglist_find_type(&meta_reglist, ET8EK8_REGLIST_MODE);
1332     format = __et8ek8_get_pad_format(sensor, fh->state, 0,
1333                      V4L2_SUBDEV_FORMAT_TRY);
1334     et8ek8_reglist_to_mbus(reglist, format);
1335 
1336     return et8ek8_set_power(sd, true);
1337 }
1338 
1339 static int et8ek8_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1340 {
1341     return et8ek8_set_power(sd, false);
1342 }
1343 
1344 static const struct v4l2_subdev_video_ops et8ek8_video_ops = {
1345     .s_stream = et8ek8_s_stream,
1346     .g_frame_interval = et8ek8_get_frame_interval,
1347     .s_frame_interval = et8ek8_set_frame_interval,
1348 };
1349 
1350 static const struct v4l2_subdev_core_ops et8ek8_core_ops = {
1351     .s_power = et8ek8_set_power,
1352 };
1353 
1354 static const struct v4l2_subdev_pad_ops et8ek8_pad_ops = {
1355     .enum_mbus_code = et8ek8_enum_mbus_code,
1356     .enum_frame_size = et8ek8_enum_frame_size,
1357     .enum_frame_interval = et8ek8_enum_frame_ival,
1358     .get_fmt = et8ek8_get_pad_format,
1359     .set_fmt = et8ek8_set_pad_format,
1360 };
1361 
1362 static const struct v4l2_subdev_ops et8ek8_ops = {
1363     .core = &et8ek8_core_ops,
1364     .video = &et8ek8_video_ops,
1365     .pad = &et8ek8_pad_ops,
1366 };
1367 
1368 static const struct v4l2_subdev_internal_ops et8ek8_internal_ops = {
1369     .registered = et8ek8_registered,
1370     .open = et8ek8_open,
1371     .close = et8ek8_close,
1372 };
1373 
1374 /* --------------------------------------------------------------------------
1375  * I2C driver
1376  */
1377 static int __maybe_unused et8ek8_suspend(struct device *dev)
1378 {
1379     struct v4l2_subdev *subdev = dev_get_drvdata(dev);
1380     struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1381 
1382     if (!sensor->power_count)
1383         return 0;
1384 
1385     return __et8ek8_set_power(sensor, false);
1386 }
1387 
1388 static int __maybe_unused et8ek8_resume(struct device *dev)
1389 {
1390     struct v4l2_subdev *subdev = dev_get_drvdata(dev);
1391     struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1392 
1393     if (!sensor->power_count)
1394         return 0;
1395 
1396     return __et8ek8_set_power(sensor, true);
1397 }
1398 
1399 static int et8ek8_probe(struct i2c_client *client)
1400 {
1401     struct et8ek8_sensor *sensor;
1402     struct device *dev = &client->dev;
1403     int ret;
1404 
1405     sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
1406     if (!sensor)
1407         return -ENOMEM;
1408 
1409     sensor->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
1410     if (IS_ERR(sensor->reset)) {
1411         dev_dbg(&client->dev, "could not request reset gpio\n");
1412         return PTR_ERR(sensor->reset);
1413     }
1414 
1415     sensor->vana = devm_regulator_get(dev, "vana");
1416     if (IS_ERR(sensor->vana)) {
1417         dev_err(&client->dev, "could not get regulator for vana\n");
1418         return PTR_ERR(sensor->vana);
1419     }
1420 
1421     sensor->ext_clk = devm_clk_get(dev, NULL);
1422     if (IS_ERR(sensor->ext_clk)) {
1423         dev_err(&client->dev, "could not get clock\n");
1424         return PTR_ERR(sensor->ext_clk);
1425     }
1426 
1427     ret = of_property_read_u32(dev->of_node, "clock-frequency",
1428                    &sensor->xclk_freq);
1429     if (ret) {
1430         dev_warn(dev, "can't get clock-frequency\n");
1431         return ret;
1432     }
1433 
1434     mutex_init(&sensor->power_lock);
1435 
1436     v4l2_i2c_subdev_init(&sensor->subdev, client, &et8ek8_ops);
1437     sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1438     sensor->subdev.internal_ops = &et8ek8_internal_ops;
1439 
1440     sensor->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1441     sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
1442     ret = media_entity_pads_init(&sensor->subdev.entity, 1, &sensor->pad);
1443     if (ret < 0) {
1444         dev_err(&client->dev, "media entity init failed!\n");
1445         goto err_mutex;
1446     }
1447 
1448     ret = v4l2_async_register_subdev_sensor(&sensor->subdev);
1449     if (ret < 0)
1450         goto err_entity;
1451 
1452     dev_dbg(dev, "initialized!\n");
1453 
1454     return 0;
1455 
1456 err_entity:
1457     media_entity_cleanup(&sensor->subdev.entity);
1458 err_mutex:
1459     mutex_destroy(&sensor->power_lock);
1460     return ret;
1461 }
1462 
1463 static int __exit et8ek8_remove(struct i2c_client *client)
1464 {
1465     struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1466     struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1467 
1468     if (sensor->power_count) {
1469         WARN_ON(1);
1470         et8ek8_power_off(sensor);
1471         sensor->power_count = 0;
1472     }
1473 
1474     v4l2_device_unregister_subdev(&sensor->subdev);
1475     device_remove_file(&client->dev, &dev_attr_priv_mem);
1476     v4l2_ctrl_handler_free(&sensor->ctrl_handler);
1477     v4l2_async_unregister_subdev(&sensor->subdev);
1478     media_entity_cleanup(&sensor->subdev.entity);
1479     mutex_destroy(&sensor->power_lock);
1480 
1481     return 0;
1482 }
1483 
1484 static const struct of_device_id et8ek8_of_table[] = {
1485     { .compatible = "toshiba,et8ek8" },
1486     { },
1487 };
1488 MODULE_DEVICE_TABLE(of, et8ek8_of_table);
1489 
1490 static const struct i2c_device_id et8ek8_id_table[] = {
1491     { ET8EK8_NAME, 0 },
1492     { }
1493 };
1494 MODULE_DEVICE_TABLE(i2c, et8ek8_id_table);
1495 
1496 static const struct dev_pm_ops et8ek8_pm_ops = {
1497     SET_SYSTEM_SLEEP_PM_OPS(et8ek8_suspend, et8ek8_resume)
1498 };
1499 
1500 static struct i2c_driver et8ek8_i2c_driver = {
1501     .driver     = {
1502         .name   = ET8EK8_NAME,
1503         .pm = &et8ek8_pm_ops,
1504         .of_match_table = et8ek8_of_table,
1505     },
1506     .probe_new  = et8ek8_probe,
1507     .remove     = __exit_p(et8ek8_remove),
1508     .id_table   = et8ek8_id_table,
1509 };
1510 
1511 module_i2c_driver(et8ek8_i2c_driver);
1512 
1513 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>, Pavel Machek <pavel@ucw.cz");
1514 MODULE_DESCRIPTION("Toshiba ET8EK8 camera sensor driver");
1515 MODULE_LICENSE("GPL");