0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <asm/unaligned.h>
0014 #include <linux/clk.h>
0015 #include <linux/delay.h>
0016 #include <linux/err.h>
0017 #include <linux/i2c.h>
0018 #include <linux/init.h>
0019 #include <linux/module.h>
0020 #include <linux/of_device.h>
0021 #include <linux/gpio/consumer.h>
0022 #include <linux/regulator/consumer.h>
0023
0024 #include <media/v4l2-common.h>
0025 #include <media/v4l2-ctrls.h>
0026 #include <media/v4l2-subdev.h>
0027
0028 #define OV2680_XVCLK_VALUE 24000000
0029
0030 #define OV2680_CHIP_ID 0x2680
0031
0032 #define OV2680_REG_STREAM_CTRL 0x0100
0033 #define OV2680_REG_SOFT_RESET 0x0103
0034
0035 #define OV2680_REG_CHIP_ID_HIGH 0x300a
0036 #define OV2680_REG_CHIP_ID_LOW 0x300b
0037
0038 #define OV2680_REG_R_MANUAL 0x3503
0039 #define OV2680_REG_GAIN_PK 0x350a
0040 #define OV2680_REG_EXPOSURE_PK_HIGH 0x3500
0041 #define OV2680_REG_TIMING_HTS 0x380c
0042 #define OV2680_REG_TIMING_VTS 0x380e
0043 #define OV2680_REG_FORMAT1 0x3820
0044 #define OV2680_REG_FORMAT2 0x3821
0045
0046 #define OV2680_REG_ISP_CTRL00 0x5080
0047
0048 #define OV2680_FRAME_RATE 30
0049
0050 #define OV2680_REG_VALUE_8BIT 1
0051 #define OV2680_REG_VALUE_16BIT 2
0052 #define OV2680_REG_VALUE_24BIT 3
0053
0054 #define OV2680_WIDTH_MAX 1600
0055 #define OV2680_HEIGHT_MAX 1200
0056
0057 enum ov2680_mode_id {
0058 OV2680_MODE_QUXGA_800_600,
0059 OV2680_MODE_720P_1280_720,
0060 OV2680_MODE_UXGA_1600_1200,
0061 OV2680_MODE_MAX,
0062 };
0063
0064 struct reg_value {
0065 u16 reg_addr;
0066 u8 val;
0067 };
0068
0069 static const char * const ov2680_supply_name[] = {
0070 "DOVDD",
0071 "DVDD",
0072 "AVDD",
0073 };
0074
0075 #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name)
0076
0077 struct ov2680_mode_info {
0078 const char *name;
0079 enum ov2680_mode_id id;
0080 u32 width;
0081 u32 height;
0082 const struct reg_value *reg_data;
0083 u32 reg_data_size;
0084 };
0085
0086 struct ov2680_ctrls {
0087 struct v4l2_ctrl_handler handler;
0088 struct {
0089 struct v4l2_ctrl *auto_exp;
0090 struct v4l2_ctrl *exposure;
0091 };
0092 struct {
0093 struct v4l2_ctrl *auto_gain;
0094 struct v4l2_ctrl *gain;
0095 };
0096
0097 struct v4l2_ctrl *hflip;
0098 struct v4l2_ctrl *vflip;
0099 struct v4l2_ctrl *test_pattern;
0100 };
0101
0102 struct ov2680_dev {
0103 struct i2c_client *i2c_client;
0104 struct v4l2_subdev sd;
0105
0106 struct media_pad pad;
0107 struct clk *xvclk;
0108 u32 xvclk_freq;
0109 struct regulator_bulk_data supplies[OV2680_NUM_SUPPLIES];
0110
0111 struct gpio_desc *reset_gpio;
0112 struct mutex lock;
0113
0114 bool mode_pending_changes;
0115 bool is_enabled;
0116 bool is_streaming;
0117
0118 struct ov2680_ctrls ctrls;
0119 struct v4l2_mbus_framefmt fmt;
0120 struct v4l2_fract frame_interval;
0121
0122 const struct ov2680_mode_info *current_mode;
0123 };
0124
0125 static const char * const test_pattern_menu[] = {
0126 "Disabled",
0127 "Color Bars",
0128 "Random Data",
0129 "Square",
0130 "Black Image",
0131 };
0132
0133 static const int ov2680_hv_flip_bayer_order[] = {
0134 MEDIA_BUS_FMT_SBGGR10_1X10,
0135 MEDIA_BUS_FMT_SGRBG10_1X10,
0136 MEDIA_BUS_FMT_SGBRG10_1X10,
0137 MEDIA_BUS_FMT_SRGGB10_1X10,
0138 };
0139
0140 static const struct reg_value ov2680_setting_30fps_QUXGA_800_600[] = {
0141 {0x3086, 0x01}, {0x370a, 0x23}, {0x3808, 0x03}, {0x3809, 0x20},
0142 {0x380a, 0x02}, {0x380b, 0x58}, {0x380c, 0x06}, {0x380d, 0xac},
0143 {0x380e, 0x02}, {0x380f, 0x84}, {0x3811, 0x04}, {0x3813, 0x04},
0144 {0x3814, 0x31}, {0x3815, 0x31}, {0x3820, 0xc0}, {0x4008, 0x00},
0145 {0x4009, 0x03}, {0x4837, 0x1e}, {0x3501, 0x4e}, {0x3502, 0xe0},
0146 };
0147
0148 static const struct reg_value ov2680_setting_30fps_720P_1280_720[] = {
0149 {0x3086, 0x00}, {0x3808, 0x05}, {0x3809, 0x00}, {0x380a, 0x02},
0150 {0x380b, 0xd0}, {0x380c, 0x06}, {0x380d, 0xa8}, {0x380e, 0x05},
0151 {0x380f, 0x0e}, {0x3811, 0x08}, {0x3813, 0x06}, {0x3814, 0x11},
0152 {0x3815, 0x11}, {0x3820, 0xc0}, {0x4008, 0x00},
0153 };
0154
0155 static const struct reg_value ov2680_setting_30fps_UXGA_1600_1200[] = {
0156 {0x3086, 0x00}, {0x3501, 0x4e}, {0x3502, 0xe0}, {0x3808, 0x06},
0157 {0x3809, 0x40}, {0x380a, 0x04}, {0x380b, 0xb0}, {0x380c, 0x06},
0158 {0x380d, 0xa8}, {0x380e, 0x05}, {0x380f, 0x0e}, {0x3811, 0x00},
0159 {0x3813, 0x00}, {0x3814, 0x11}, {0x3815, 0x11}, {0x3820, 0xc0},
0160 {0x4008, 0x00}, {0x4837, 0x18}
0161 };
0162
0163 static const struct ov2680_mode_info ov2680_mode_init_data = {
0164 "mode_quxga_800_600", OV2680_MODE_QUXGA_800_600, 800, 600,
0165 ov2680_setting_30fps_QUXGA_800_600,
0166 ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600),
0167 };
0168
0169 static const struct ov2680_mode_info ov2680_mode_data[OV2680_MODE_MAX] = {
0170 {"mode_quxga_800_600", OV2680_MODE_QUXGA_800_600,
0171 800, 600, ov2680_setting_30fps_QUXGA_800_600,
0172 ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600)},
0173 {"mode_720p_1280_720", OV2680_MODE_720P_1280_720,
0174 1280, 720, ov2680_setting_30fps_720P_1280_720,
0175 ARRAY_SIZE(ov2680_setting_30fps_720P_1280_720)},
0176 {"mode_uxga_1600_1200", OV2680_MODE_UXGA_1600_1200,
0177 1600, 1200, ov2680_setting_30fps_UXGA_1600_1200,
0178 ARRAY_SIZE(ov2680_setting_30fps_UXGA_1600_1200)},
0179 };
0180
0181 static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd)
0182 {
0183 return container_of(sd, struct ov2680_dev, sd);
0184 }
0185
0186 static struct device *ov2680_to_dev(struct ov2680_dev *sensor)
0187 {
0188 return &sensor->i2c_client->dev;
0189 }
0190
0191 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
0192 {
0193 return &container_of(ctrl->handler, struct ov2680_dev,
0194 ctrls.handler)->sd;
0195 }
0196
0197 static int __ov2680_write_reg(struct ov2680_dev *sensor, u16 reg,
0198 unsigned int len, u32 val)
0199 {
0200 struct i2c_client *client = sensor->i2c_client;
0201 u8 buf[6];
0202 int ret;
0203
0204 if (len > 4)
0205 return -EINVAL;
0206
0207 put_unaligned_be16(reg, buf);
0208 put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
0209 ret = i2c_master_send(client, buf, len + 2);
0210 if (ret != len + 2) {
0211 dev_err(&client->dev, "write error: reg=0x%4x: %d\n", reg, ret);
0212 return -EIO;
0213 }
0214
0215 return 0;
0216 }
0217
0218 #define ov2680_write_reg(s, r, v) \
0219 __ov2680_write_reg(s, r, OV2680_REG_VALUE_8BIT, v)
0220
0221 #define ov2680_write_reg16(s, r, v) \
0222 __ov2680_write_reg(s, r, OV2680_REG_VALUE_16BIT, v)
0223
0224 #define ov2680_write_reg24(s, r, v) \
0225 __ov2680_write_reg(s, r, OV2680_REG_VALUE_24BIT, v)
0226
0227 static int __ov2680_read_reg(struct ov2680_dev *sensor, u16 reg,
0228 unsigned int len, u32 *val)
0229 {
0230 struct i2c_client *client = sensor->i2c_client;
0231 struct i2c_msg msgs[2];
0232 u8 addr_buf[2] = { reg >> 8, reg & 0xff };
0233 u8 data_buf[4] = { 0, };
0234 int ret;
0235
0236 if (len > 4)
0237 return -EINVAL;
0238
0239 msgs[0].addr = client->addr;
0240 msgs[0].flags = 0;
0241 msgs[0].len = ARRAY_SIZE(addr_buf);
0242 msgs[0].buf = addr_buf;
0243
0244 msgs[1].addr = client->addr;
0245 msgs[1].flags = I2C_M_RD;
0246 msgs[1].len = len;
0247 msgs[1].buf = &data_buf[4 - len];
0248
0249 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
0250 if (ret != ARRAY_SIZE(msgs)) {
0251 dev_err(&client->dev, "read error: reg=0x%4x: %d\n", reg, ret);
0252 return -EIO;
0253 }
0254
0255 *val = get_unaligned_be32(data_buf);
0256
0257 return 0;
0258 }
0259
0260 #define ov2680_read_reg(s, r, v) \
0261 __ov2680_read_reg(s, r, OV2680_REG_VALUE_8BIT, v)
0262
0263 #define ov2680_read_reg16(s, r, v) \
0264 __ov2680_read_reg(s, r, OV2680_REG_VALUE_16BIT, v)
0265
0266 #define ov2680_read_reg24(s, r, v) \
0267 __ov2680_read_reg(s, r, OV2680_REG_VALUE_24BIT, v)
0268
0269 static int ov2680_mod_reg(struct ov2680_dev *sensor, u16 reg, u8 mask, u8 val)
0270 {
0271 u32 readval;
0272 int ret;
0273
0274 ret = ov2680_read_reg(sensor, reg, &readval);
0275 if (ret < 0)
0276 return ret;
0277
0278 readval &= ~mask;
0279 val &= mask;
0280 val |= readval;
0281
0282 return ov2680_write_reg(sensor, reg, val);
0283 }
0284
0285 static int ov2680_load_regs(struct ov2680_dev *sensor,
0286 const struct ov2680_mode_info *mode)
0287 {
0288 const struct reg_value *regs = mode->reg_data;
0289 unsigned int i;
0290 int ret = 0;
0291 u16 reg_addr;
0292 u8 val;
0293
0294 for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
0295 reg_addr = regs->reg_addr;
0296 val = regs->val;
0297
0298 ret = ov2680_write_reg(sensor, reg_addr, val);
0299 if (ret)
0300 break;
0301 }
0302
0303 return ret;
0304 }
0305
0306 static void ov2680_power_up(struct ov2680_dev *sensor)
0307 {
0308 if (!sensor->reset_gpio)
0309 return;
0310
0311 gpiod_set_value(sensor->reset_gpio, 0);
0312 usleep_range(5000, 10000);
0313 }
0314
0315 static void ov2680_power_down(struct ov2680_dev *sensor)
0316 {
0317 if (!sensor->reset_gpio)
0318 return;
0319
0320 gpiod_set_value(sensor->reset_gpio, 1);
0321 usleep_range(5000, 10000);
0322 }
0323
0324 static int ov2680_bayer_order(struct ov2680_dev *sensor)
0325 {
0326 u32 format1;
0327 u32 format2;
0328 u32 hv_flip;
0329 int ret;
0330
0331 ret = ov2680_read_reg(sensor, OV2680_REG_FORMAT1, &format1);
0332 if (ret < 0)
0333 return ret;
0334
0335 ret = ov2680_read_reg(sensor, OV2680_REG_FORMAT2, &format2);
0336 if (ret < 0)
0337 return ret;
0338
0339 hv_flip = (format2 & BIT(2) << 1) | (format1 & BIT(2));
0340
0341 sensor->fmt.code = ov2680_hv_flip_bayer_order[hv_flip];
0342
0343 return 0;
0344 }
0345
0346 static int ov2680_vflip_enable(struct ov2680_dev *sensor)
0347 {
0348 int ret;
0349
0350 ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, BIT(2), BIT(2));
0351 if (ret < 0)
0352 return ret;
0353
0354 return ov2680_bayer_order(sensor);
0355 }
0356
0357 static int ov2680_vflip_disable(struct ov2680_dev *sensor)
0358 {
0359 int ret;
0360
0361 ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, BIT(2), BIT(0));
0362 if (ret < 0)
0363 return ret;
0364
0365 return ov2680_bayer_order(sensor);
0366 }
0367
0368 static int ov2680_hflip_enable(struct ov2680_dev *sensor)
0369 {
0370 int ret;
0371
0372 ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, BIT(2), BIT(2));
0373 if (ret < 0)
0374 return ret;
0375
0376 return ov2680_bayer_order(sensor);
0377 }
0378
0379 static int ov2680_hflip_disable(struct ov2680_dev *sensor)
0380 {
0381 int ret;
0382
0383 ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, BIT(2), BIT(0));
0384 if (ret < 0)
0385 return ret;
0386
0387 return ov2680_bayer_order(sensor);
0388 }
0389
0390 static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
0391 {
0392 int ret;
0393
0394 if (!value)
0395 return ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), 0);
0396
0397 ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, 0x03, value - 1);
0398 if (ret < 0)
0399 return ret;
0400
0401 ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), BIT(7));
0402 if (ret < 0)
0403 return ret;
0404
0405 return 0;
0406 }
0407
0408 static int ov2680_gain_set(struct ov2680_dev *sensor, bool auto_gain)
0409 {
0410 struct ov2680_ctrls *ctrls = &sensor->ctrls;
0411 u32 gain;
0412 int ret;
0413
0414 ret = ov2680_mod_reg(sensor, OV2680_REG_R_MANUAL, BIT(1),
0415 auto_gain ? 0 : BIT(1));
0416 if (ret < 0)
0417 return ret;
0418
0419 if (auto_gain || !ctrls->gain->is_new)
0420 return 0;
0421
0422 gain = ctrls->gain->val;
0423
0424 ret = ov2680_write_reg16(sensor, OV2680_REG_GAIN_PK, gain);
0425
0426 return 0;
0427 }
0428
0429 static int ov2680_gain_get(struct ov2680_dev *sensor)
0430 {
0431 u32 gain;
0432 int ret;
0433
0434 ret = ov2680_read_reg16(sensor, OV2680_REG_GAIN_PK, &gain);
0435 if (ret)
0436 return ret;
0437
0438 return gain;
0439 }
0440
0441 static int ov2680_exposure_set(struct ov2680_dev *sensor, bool auto_exp)
0442 {
0443 struct ov2680_ctrls *ctrls = &sensor->ctrls;
0444 u32 exp;
0445 int ret;
0446
0447 ret = ov2680_mod_reg(sensor, OV2680_REG_R_MANUAL, BIT(0),
0448 auto_exp ? 0 : BIT(0));
0449 if (ret < 0)
0450 return ret;
0451
0452 if (auto_exp || !ctrls->exposure->is_new)
0453 return 0;
0454
0455 exp = (u32)ctrls->exposure->val;
0456 exp <<= 4;
0457
0458 return ov2680_write_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH, exp);
0459 }
0460
0461 static int ov2680_exposure_get(struct ov2680_dev *sensor)
0462 {
0463 int ret;
0464 u32 exp;
0465
0466 ret = ov2680_read_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH, &exp);
0467 if (ret)
0468 return ret;
0469
0470 return exp >> 4;
0471 }
0472
0473 static int ov2680_stream_enable(struct ov2680_dev *sensor)
0474 {
0475 return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 1);
0476 }
0477
0478 static int ov2680_stream_disable(struct ov2680_dev *sensor)
0479 {
0480 return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 0);
0481 }
0482
0483 static int ov2680_mode_set(struct ov2680_dev *sensor)
0484 {
0485 struct ov2680_ctrls *ctrls = &sensor->ctrls;
0486 int ret;
0487
0488 ret = ov2680_gain_set(sensor, false);
0489 if (ret < 0)
0490 return ret;
0491
0492 ret = ov2680_exposure_set(sensor, false);
0493 if (ret < 0)
0494 return ret;
0495
0496 ret = ov2680_load_regs(sensor, sensor->current_mode);
0497 if (ret < 0)
0498 return ret;
0499
0500 if (ctrls->auto_gain->val) {
0501 ret = ov2680_gain_set(sensor, true);
0502 if (ret < 0)
0503 return ret;
0504 }
0505
0506 if (ctrls->auto_exp->val == V4L2_EXPOSURE_AUTO) {
0507 ret = ov2680_exposure_set(sensor, true);
0508 if (ret < 0)
0509 return ret;
0510 }
0511
0512 sensor->mode_pending_changes = false;
0513
0514 return 0;
0515 }
0516
0517 static int ov2680_mode_restore(struct ov2680_dev *sensor)
0518 {
0519 int ret;
0520
0521 ret = ov2680_load_regs(sensor, &ov2680_mode_init_data);
0522 if (ret < 0)
0523 return ret;
0524
0525 return ov2680_mode_set(sensor);
0526 }
0527
0528 static int ov2680_power_off(struct ov2680_dev *sensor)
0529 {
0530 if (!sensor->is_enabled)
0531 return 0;
0532
0533 clk_disable_unprepare(sensor->xvclk);
0534 ov2680_power_down(sensor);
0535 regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
0536 sensor->is_enabled = false;
0537
0538 return 0;
0539 }
0540
0541 static int ov2680_power_on(struct ov2680_dev *sensor)
0542 {
0543 struct device *dev = ov2680_to_dev(sensor);
0544 int ret;
0545
0546 if (sensor->is_enabled)
0547 return 0;
0548
0549 ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies);
0550 if (ret < 0) {
0551 dev_err(dev, "failed to enable regulators: %d\n", ret);
0552 return ret;
0553 }
0554
0555 if (!sensor->reset_gpio) {
0556 ret = ov2680_write_reg(sensor, OV2680_REG_SOFT_RESET, 0x01);
0557 if (ret != 0) {
0558 dev_err(dev, "sensor soft reset failed\n");
0559 return ret;
0560 }
0561 usleep_range(1000, 2000);
0562 } else {
0563 ov2680_power_down(sensor);
0564 ov2680_power_up(sensor);
0565 }
0566
0567 ret = clk_prepare_enable(sensor->xvclk);
0568 if (ret < 0)
0569 return ret;
0570
0571 sensor->is_enabled = true;
0572
0573
0574 ov2680_stream_enable(sensor);
0575 usleep_range(1000, 2000);
0576 ov2680_stream_disable(sensor);
0577
0578 return 0;
0579 }
0580
0581 static int ov2680_s_power(struct v4l2_subdev *sd, int on)
0582 {
0583 struct ov2680_dev *sensor = to_ov2680_dev(sd);
0584 int ret = 0;
0585
0586 mutex_lock(&sensor->lock);
0587
0588 if (on)
0589 ret = ov2680_power_on(sensor);
0590 else
0591 ret = ov2680_power_off(sensor);
0592
0593 mutex_unlock(&sensor->lock);
0594
0595 if (on && ret == 0) {
0596 ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
0597 if (ret < 0)
0598 return ret;
0599
0600 ret = ov2680_mode_restore(sensor);
0601 }
0602
0603 return ret;
0604 }
0605
0606 static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd,
0607 struct v4l2_subdev_frame_interval *fi)
0608 {
0609 struct ov2680_dev *sensor = to_ov2680_dev(sd);
0610
0611 mutex_lock(&sensor->lock);
0612 fi->interval = sensor->frame_interval;
0613 mutex_unlock(&sensor->lock);
0614
0615 return 0;
0616 }
0617
0618 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
0619 {
0620 struct ov2680_dev *sensor = to_ov2680_dev(sd);
0621 int ret = 0;
0622
0623 mutex_lock(&sensor->lock);
0624
0625 if (sensor->is_streaming == !!enable)
0626 goto unlock;
0627
0628 if (enable && sensor->mode_pending_changes) {
0629 ret = ov2680_mode_set(sensor);
0630 if (ret < 0)
0631 goto unlock;
0632 }
0633
0634 if (enable)
0635 ret = ov2680_stream_enable(sensor);
0636 else
0637 ret = ov2680_stream_disable(sensor);
0638
0639 sensor->is_streaming = !!enable;
0640
0641 unlock:
0642 mutex_unlock(&sensor->lock);
0643
0644 return ret;
0645 }
0646
0647 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
0648 struct v4l2_subdev_state *sd_state,
0649 struct v4l2_subdev_mbus_code_enum *code)
0650 {
0651 struct ov2680_dev *sensor = to_ov2680_dev(sd);
0652
0653 if (code->pad != 0 || code->index != 0)
0654 return -EINVAL;
0655
0656 code->code = sensor->fmt.code;
0657
0658 return 0;
0659 }
0660
0661 static int ov2680_get_fmt(struct v4l2_subdev *sd,
0662 struct v4l2_subdev_state *sd_state,
0663 struct v4l2_subdev_format *format)
0664 {
0665 struct ov2680_dev *sensor = to_ov2680_dev(sd);
0666 struct v4l2_mbus_framefmt *fmt = NULL;
0667 int ret = 0;
0668
0669 if (format->pad != 0)
0670 return -EINVAL;
0671
0672 mutex_lock(&sensor->lock);
0673
0674 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
0675 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
0676 fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
0677 format->pad);
0678 #else
0679 ret = -EINVAL;
0680 #endif
0681 } else {
0682 fmt = &sensor->fmt;
0683 }
0684
0685 if (fmt)
0686 format->format = *fmt;
0687
0688 mutex_unlock(&sensor->lock);
0689
0690 return ret;
0691 }
0692
0693 static int ov2680_set_fmt(struct v4l2_subdev *sd,
0694 struct v4l2_subdev_state *sd_state,
0695 struct v4l2_subdev_format *format)
0696 {
0697 struct ov2680_dev *sensor = to_ov2680_dev(sd);
0698 struct v4l2_mbus_framefmt *fmt = &format->format;
0699 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
0700 struct v4l2_mbus_framefmt *try_fmt;
0701 #endif
0702 const struct ov2680_mode_info *mode;
0703 int ret = 0;
0704
0705 if (format->pad != 0)
0706 return -EINVAL;
0707
0708 mutex_lock(&sensor->lock);
0709
0710 if (sensor->is_streaming) {
0711 ret = -EBUSY;
0712 goto unlock;
0713 }
0714
0715 mode = v4l2_find_nearest_size(ov2680_mode_data,
0716 ARRAY_SIZE(ov2680_mode_data), width,
0717 height, fmt->width, fmt->height);
0718 if (!mode) {
0719 ret = -EINVAL;
0720 goto unlock;
0721 }
0722
0723 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
0724 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
0725 try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
0726 format->format = *try_fmt;
0727 #endif
0728 goto unlock;
0729 }
0730
0731 fmt->width = mode->width;
0732 fmt->height = mode->height;
0733 fmt->code = sensor->fmt.code;
0734 fmt->colorspace = sensor->fmt.colorspace;
0735
0736 sensor->current_mode = mode;
0737 sensor->fmt = format->format;
0738 sensor->mode_pending_changes = true;
0739
0740 unlock:
0741 mutex_unlock(&sensor->lock);
0742
0743 return ret;
0744 }
0745
0746 static int ov2680_init_cfg(struct v4l2_subdev *sd,
0747 struct v4l2_subdev_state *sd_state)
0748 {
0749 struct v4l2_subdev_format fmt = {
0750 .which = sd_state ? V4L2_SUBDEV_FORMAT_TRY
0751 : V4L2_SUBDEV_FORMAT_ACTIVE,
0752 .format = {
0753 .width = 800,
0754 .height = 600,
0755 }
0756 };
0757
0758 return ov2680_set_fmt(sd, sd_state, &fmt);
0759 }
0760
0761 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
0762 struct v4l2_subdev_state *sd_state,
0763 struct v4l2_subdev_frame_size_enum *fse)
0764 {
0765 int index = fse->index;
0766
0767 if (index >= OV2680_MODE_MAX || index < 0)
0768 return -EINVAL;
0769
0770 fse->min_width = ov2680_mode_data[index].width;
0771 fse->min_height = ov2680_mode_data[index].height;
0772 fse->max_width = ov2680_mode_data[index].width;
0773 fse->max_height = ov2680_mode_data[index].height;
0774
0775 return 0;
0776 }
0777
0778 static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
0779 struct v4l2_subdev_state *sd_state,
0780 struct v4l2_subdev_frame_interval_enum *fie)
0781 {
0782 struct v4l2_fract tpf;
0783
0784 if (fie->index >= OV2680_MODE_MAX || fie->width > OV2680_WIDTH_MAX ||
0785 fie->height > OV2680_HEIGHT_MAX ||
0786 fie->which > V4L2_SUBDEV_FORMAT_ACTIVE)
0787 return -EINVAL;
0788
0789 tpf.denominator = OV2680_FRAME_RATE;
0790 tpf.numerator = 1;
0791
0792 fie->interval = tpf;
0793
0794 return 0;
0795 }
0796
0797 static int ov2680_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
0798 {
0799 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
0800 struct ov2680_dev *sensor = to_ov2680_dev(sd);
0801 struct ov2680_ctrls *ctrls = &sensor->ctrls;
0802 int val;
0803
0804 if (!sensor->is_enabled)
0805 return 0;
0806
0807 switch (ctrl->id) {
0808 case V4L2_CID_GAIN:
0809 val = ov2680_gain_get(sensor);
0810 if (val < 0)
0811 return val;
0812 ctrls->gain->val = val;
0813 break;
0814 case V4L2_CID_EXPOSURE:
0815 val = ov2680_exposure_get(sensor);
0816 if (val < 0)
0817 return val;
0818 ctrls->exposure->val = val;
0819 break;
0820 }
0821
0822 return 0;
0823 }
0824
0825 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
0826 {
0827 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
0828 struct ov2680_dev *sensor = to_ov2680_dev(sd);
0829 struct ov2680_ctrls *ctrls = &sensor->ctrls;
0830
0831 if (!sensor->is_enabled)
0832 return 0;
0833
0834 switch (ctrl->id) {
0835 case V4L2_CID_AUTOGAIN:
0836 return ov2680_gain_set(sensor, !!ctrl->val);
0837 case V4L2_CID_GAIN:
0838 return ov2680_gain_set(sensor, !!ctrls->auto_gain->val);
0839 case V4L2_CID_EXPOSURE_AUTO:
0840 return ov2680_exposure_set(sensor, !!ctrl->val);
0841 case V4L2_CID_EXPOSURE:
0842 return ov2680_exposure_set(sensor, !!ctrls->auto_exp->val);
0843 case V4L2_CID_VFLIP:
0844 if (sensor->is_streaming)
0845 return -EBUSY;
0846 if (ctrl->val)
0847 return ov2680_vflip_enable(sensor);
0848 else
0849 return ov2680_vflip_disable(sensor);
0850 case V4L2_CID_HFLIP:
0851 if (sensor->is_streaming)
0852 return -EBUSY;
0853 if (ctrl->val)
0854 return ov2680_hflip_enable(sensor);
0855 else
0856 return ov2680_hflip_disable(sensor);
0857 case V4L2_CID_TEST_PATTERN:
0858 return ov2680_test_pattern_set(sensor, ctrl->val);
0859 default:
0860 break;
0861 }
0862
0863 return -EINVAL;
0864 }
0865
0866 static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
0867 .g_volatile_ctrl = ov2680_g_volatile_ctrl,
0868 .s_ctrl = ov2680_s_ctrl,
0869 };
0870
0871 static const struct v4l2_subdev_core_ops ov2680_core_ops = {
0872 .s_power = ov2680_s_power,
0873 };
0874
0875 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
0876 .g_frame_interval = ov2680_s_g_frame_interval,
0877 .s_frame_interval = ov2680_s_g_frame_interval,
0878 .s_stream = ov2680_s_stream,
0879 };
0880
0881 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
0882 .init_cfg = ov2680_init_cfg,
0883 .enum_mbus_code = ov2680_enum_mbus_code,
0884 .get_fmt = ov2680_get_fmt,
0885 .set_fmt = ov2680_set_fmt,
0886 .enum_frame_size = ov2680_enum_frame_size,
0887 .enum_frame_interval = ov2680_enum_frame_interval,
0888 };
0889
0890 static const struct v4l2_subdev_ops ov2680_subdev_ops = {
0891 .core = &ov2680_core_ops,
0892 .video = &ov2680_video_ops,
0893 .pad = &ov2680_pad_ops,
0894 };
0895
0896 static int ov2680_mode_init(struct ov2680_dev *sensor)
0897 {
0898 const struct ov2680_mode_info *init_mode;
0899
0900
0901 sensor->fmt.code = MEDIA_BUS_FMT_SBGGR10_1X10;
0902 sensor->fmt.width = 800;
0903 sensor->fmt.height = 600;
0904 sensor->fmt.field = V4L2_FIELD_NONE;
0905 sensor->fmt.colorspace = V4L2_COLORSPACE_SRGB;
0906
0907 sensor->frame_interval.denominator = OV2680_FRAME_RATE;
0908 sensor->frame_interval.numerator = 1;
0909
0910 init_mode = &ov2680_mode_init_data;
0911
0912 sensor->current_mode = init_mode;
0913
0914 sensor->mode_pending_changes = true;
0915
0916 return 0;
0917 }
0918
0919 static int ov2680_v4l2_register(struct ov2680_dev *sensor)
0920 {
0921 const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
0922 struct ov2680_ctrls *ctrls = &sensor->ctrls;
0923 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
0924 int ret = 0;
0925
0926 v4l2_i2c_subdev_init(&sensor->sd, sensor->i2c_client,
0927 &ov2680_subdev_ops);
0928
0929 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
0930 sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
0931 #endif
0932 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
0933 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
0934
0935 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
0936 if (ret < 0)
0937 return ret;
0938
0939 v4l2_ctrl_handler_init(hdl, 7);
0940
0941 hdl->lock = &sensor->lock;
0942
0943 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
0944 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
0945
0946 ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl,
0947 &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
0948 ARRAY_SIZE(test_pattern_menu) - 1,
0949 0, 0, test_pattern_menu);
0950
0951 ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
0952 V4L2_CID_EXPOSURE_AUTO,
0953 V4L2_EXPOSURE_MANUAL, 0,
0954 V4L2_EXPOSURE_AUTO);
0955
0956 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
0957 0, 32767, 1, 0);
0958
0959 ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
0960 0, 1, 1, 1);
0961 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 2047, 1, 0);
0962
0963 if (hdl->error) {
0964 ret = hdl->error;
0965 goto cleanup_entity;
0966 }
0967
0968 ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
0969 ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
0970
0971 v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
0972 v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
0973
0974 sensor->sd.ctrl_handler = hdl;
0975
0976 ret = v4l2_async_register_subdev(&sensor->sd);
0977 if (ret < 0)
0978 goto cleanup_entity;
0979
0980 return 0;
0981
0982 cleanup_entity:
0983 media_entity_cleanup(&sensor->sd.entity);
0984 v4l2_ctrl_handler_free(hdl);
0985
0986 return ret;
0987 }
0988
0989 static int ov2680_get_regulators(struct ov2680_dev *sensor)
0990 {
0991 int i;
0992
0993 for (i = 0; i < OV2680_NUM_SUPPLIES; i++)
0994 sensor->supplies[i].supply = ov2680_supply_name[i];
0995
0996 return devm_regulator_bulk_get(&sensor->i2c_client->dev,
0997 OV2680_NUM_SUPPLIES,
0998 sensor->supplies);
0999 }
1000
1001 static int ov2680_check_id(struct ov2680_dev *sensor)
1002 {
1003 struct device *dev = ov2680_to_dev(sensor);
1004 u32 chip_id;
1005 int ret;
1006
1007 ov2680_power_on(sensor);
1008
1009 ret = ov2680_read_reg16(sensor, OV2680_REG_CHIP_ID_HIGH, &chip_id);
1010 if (ret < 0) {
1011 dev_err(dev, "failed to read chip id high\n");
1012 return -ENODEV;
1013 }
1014
1015 if (chip_id != OV2680_CHIP_ID) {
1016 dev_err(dev, "chip id: 0x%04x does not match expected 0x%04x\n",
1017 chip_id, OV2680_CHIP_ID);
1018 return -ENODEV;
1019 }
1020
1021 return 0;
1022 }
1023
1024 static int ov2680_parse_dt(struct ov2680_dev *sensor)
1025 {
1026 struct device *dev = ov2680_to_dev(sensor);
1027 int ret;
1028
1029 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1030 GPIOD_OUT_HIGH);
1031 ret = PTR_ERR_OR_ZERO(sensor->reset_gpio);
1032 if (ret < 0) {
1033 dev_dbg(dev, "error while getting reset gpio: %d\n", ret);
1034 return ret;
1035 }
1036
1037 sensor->xvclk = devm_clk_get(dev, "xvclk");
1038 if (IS_ERR(sensor->xvclk)) {
1039 dev_err(dev, "xvclk clock missing or invalid\n");
1040 return PTR_ERR(sensor->xvclk);
1041 }
1042
1043 sensor->xvclk_freq = clk_get_rate(sensor->xvclk);
1044 if (sensor->xvclk_freq != OV2680_XVCLK_VALUE) {
1045 dev_err(dev, "wrong xvclk frequency %d HZ, expected: %d Hz\n",
1046 sensor->xvclk_freq, OV2680_XVCLK_VALUE);
1047 return -EINVAL;
1048 }
1049
1050 return 0;
1051 }
1052
1053 static int ov2680_probe(struct i2c_client *client)
1054 {
1055 struct device *dev = &client->dev;
1056 struct ov2680_dev *sensor;
1057 int ret;
1058
1059 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
1060 if (!sensor)
1061 return -ENOMEM;
1062
1063 sensor->i2c_client = client;
1064
1065 ret = ov2680_parse_dt(sensor);
1066 if (ret < 0)
1067 return -EINVAL;
1068
1069 ret = ov2680_mode_init(sensor);
1070 if (ret < 0)
1071 return ret;
1072
1073 ret = ov2680_get_regulators(sensor);
1074 if (ret < 0) {
1075 dev_err(dev, "failed to get regulators\n");
1076 return ret;
1077 }
1078
1079 mutex_init(&sensor->lock);
1080
1081 ret = ov2680_check_id(sensor);
1082 if (ret < 0)
1083 goto lock_destroy;
1084
1085 ret = ov2680_v4l2_register(sensor);
1086 if (ret < 0)
1087 goto lock_destroy;
1088
1089 dev_info(dev, "ov2680 init correctly\n");
1090
1091 return 0;
1092
1093 lock_destroy:
1094 dev_err(dev, "ov2680 init fail: %d\n", ret);
1095 mutex_destroy(&sensor->lock);
1096
1097 return ret;
1098 }
1099
1100 static int ov2680_remove(struct i2c_client *client)
1101 {
1102 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1103 struct ov2680_dev *sensor = to_ov2680_dev(sd);
1104
1105 v4l2_async_unregister_subdev(&sensor->sd);
1106 mutex_destroy(&sensor->lock);
1107 media_entity_cleanup(&sensor->sd.entity);
1108 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
1109
1110 return 0;
1111 }
1112
1113 static int __maybe_unused ov2680_suspend(struct device *dev)
1114 {
1115 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1116 struct ov2680_dev *sensor = to_ov2680_dev(sd);
1117
1118 if (sensor->is_streaming)
1119 ov2680_stream_disable(sensor);
1120
1121 return 0;
1122 }
1123
1124 static int __maybe_unused ov2680_resume(struct device *dev)
1125 {
1126 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1127 struct ov2680_dev *sensor = to_ov2680_dev(sd);
1128 int ret;
1129
1130 if (sensor->is_streaming) {
1131 ret = ov2680_stream_enable(sensor);
1132 if (ret < 0)
1133 goto stream_disable;
1134 }
1135
1136 return 0;
1137
1138 stream_disable:
1139 ov2680_stream_disable(sensor);
1140 sensor->is_streaming = false;
1141
1142 return ret;
1143 }
1144
1145 static const struct dev_pm_ops ov2680_pm_ops = {
1146 SET_SYSTEM_SLEEP_PM_OPS(ov2680_suspend, ov2680_resume)
1147 };
1148
1149 static const struct of_device_id ov2680_dt_ids[] = {
1150 { .compatible = "ovti,ov2680" },
1151 { },
1152 };
1153 MODULE_DEVICE_TABLE(of, ov2680_dt_ids);
1154
1155 static struct i2c_driver ov2680_i2c_driver = {
1156 .driver = {
1157 .name = "ov2680",
1158 .pm = &ov2680_pm_ops,
1159 .of_match_table = of_match_ptr(ov2680_dt_ids),
1160 },
1161 .probe_new = ov2680_probe,
1162 .remove = ov2680_remove,
1163 };
1164 module_i2c_driver(ov2680_i2c_driver);
1165
1166 MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>");
1167 MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");
1168 MODULE_LICENSE("GPL v2");