1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2022 Intel Corporation.
4 #include <linux/unaligned.h>
5 #include <linux/acpi.h>
8 #include <linux/gpio/consumer.h>
9 #include <linux/module.h>
10 #include <linux/delay.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/regulator/consumer.h>
13 #include <media/v4l2-ctrls.h>
14 #include <media/v4l2-device.h>
15 #include <media/v4l2-fwnode.h>
17 #define OV08X40_REG_VALUE_08BIT 1
18 #define OV08X40_REG_VALUE_16BIT 2
19 #define OV08X40_REG_VALUE_24BIT 3
21 #define OV08X40_REG_MODE_SELECT 0x0100
22 #define OV08X40_MODE_STANDBY 0x00
23 #define OV08X40_MODE_STREAMING 0x01
25 #define OV08X40_REG_AO_STANDBY 0x1000
26 #define OV08X40_AO_STREAMING 0x04
28 #define OV08X40_REG_MS_SELECT 0x1001
29 #define OV08X40_MS_STANDBY 0x00
30 #define OV08X40_MS_STREAMING 0x04
32 #define OV08X40_REG_SOFTWARE_RST 0x0103
33 #define OV08X40_SOFTWARE_RST 0x01
36 #define OV08X40_REG_CHIP_ID 0x300a
37 #define OV08X40_CHIP_ID 0x560858
39 /* V_TIMING internal */
40 #define OV08X40_REG_VTS 0x380e
41 #define OV08X40_VTS_30FPS 0x09c4 /* the VTS need to be half in normal mode */
42 #define OV08X40_VTS_BIN_30FPS 0x115c
43 #define OV08X40_VTS_MAX 0x7fff
45 /* H TIMING internal */
46 #define OV08X40_REG_HTS 0x380c
47 #define OV08X40_HTS_30FPS 0x0280
49 /* Exposure control */
50 #define OV08X40_REG_EXPOSURE 0x3500
51 #define OV08X40_EXPOSURE_MAX_MARGIN 8
52 #define OV08X40_EXPOSURE_BIN_MAX_MARGIN 2
53 #define OV08X40_EXPOSURE_MIN 4
54 #define OV08X40_EXPOSURE_STEP 1
55 #define OV08X40_EXPOSURE_DEFAULT 0x40
57 /* Short Exposure control */
58 #define OV08X40_REG_SHORT_EXPOSURE 0x3540
60 /* Analog gain control */
61 #define OV08X40_REG_ANALOG_GAIN 0x3508
62 #define OV08X40_ANA_GAIN_MIN 0x80
63 #define OV08X40_ANA_GAIN_MAX 0x07c0
64 #define OV08X40_ANA_GAIN_STEP 1
65 #define OV08X40_ANA_GAIN_DEFAULT 0x80
67 /* Digital gain control */
68 #define OV08X40_REG_DGTL_GAIN_H 0x350a
69 #define OV08X40_REG_DGTL_GAIN_M 0x350b
70 #define OV08X40_REG_DGTL_GAIN_L 0x350c
72 #define OV08X40_DGTL_GAIN_MIN 1024 /* Min = 1 X */
73 #define OV08X40_DGTL_GAIN_MAX (4096 - 1) /* Max = 4 X */
74 #define OV08X40_DGTL_GAIN_DEFAULT 2560 /* Default gain = 2.5 X */
75 #define OV08X40_DGTL_GAIN_STEP 1 /* Each step = 1/1024 */
77 #define OV08X40_DGTL_GAIN_L_SHIFT 6
78 #define OV08X40_DGTL_GAIN_L_MASK 0x3
79 #define OV08X40_DGTL_GAIN_M_SHIFT 2
80 #define OV08X40_DGTL_GAIN_M_MASK 0xff
81 #define OV08X40_DGTL_GAIN_H_SHIFT 10
82 #define OV08X40_DGTL_GAIN_H_MASK 0x1F
84 /* Test Pattern Control */
85 #define OV08X40_REG_TEST_PATTERN 0x50C1
86 #define OV08X40_REG_ISP 0x5000
87 #define OV08X40_REG_SHORT_TEST_PATTERN 0x53C1
88 #define OV08X40_TEST_PATTERN_ENABLE BIT(0)
89 #define OV08X40_TEST_PATTERN_MASK 0xcf
90 #define OV08X40_TEST_PATTERN_BAR_SHIFT 4
93 #define OV08X40_REG_VFLIP 0x3820
94 #define OV08X40_REG_MIRROR 0x3821
96 /* Horizontal Window Offset */
97 #define OV08X40_REG_H_WIN_OFFSET 0x3811
99 /* Vertical Window Offset */
100 #define OV08X40_REG_V_WIN_OFFSET 0x3813
103 #define OV08X40_REG_XTALK_FIRST_A 0x5a80
104 #define OV08X40_REG_XTALK_LAST_A 0x5b9f
105 #define OV08X40_REG_XTALK_FIRST_B 0x5bc0
106 #define OV08X40_REG_XTALK_LAST_B 0x5f1f
109 OV08X40_LINK_FREQ_400MHZ_INDEX
,
117 struct ov08x40_reg_list
{
119 const struct ov08x40_reg
*regs
;
122 /* Link frequency config */
123 struct ov08x40_link_freq_config
{
124 /* registers for this link frequency */
125 struct ov08x40_reg_list reg_list
;
128 /* Mode : resolution and related config&values */
129 struct ov08x40_mode
{
140 /* Line Length Pixels */
143 /* Index of Link frequency config to be used */
145 /* Default register values */
146 struct ov08x40_reg_list reg_list
;
148 /* Exposure calculation */
153 static const struct ov08x40_reg mipi_data_rate_800mbps
[] = {
173 static const struct ov08x40_reg mode_3856x2416_regs
[] = {
685 static const struct ov08x40_reg mode_1928x1208_regs
[] = {
1210 static const char * const ov08x40_test_pattern_menu
[] = {
1212 "Vertical Color Bar Type 1",
1213 "Vertical Color Bar Type 2",
1214 "Vertical Color Bar Type 3",
1215 "Vertical Color Bar Type 4"
1218 /* Configurations for supported link frequencies */
1219 #define OV08X40_LINK_FREQ_400MHZ 400000000ULL
1220 #define OV08X40_SCLK_96MHZ 96000000ULL
1221 #define OV08X40_XVCLK 19200000
1222 #define OV08X40_DATA_LANES 4
1225 * pixel_rate = link_freq * data-rate * nr_of_lanes / bits_per_sample
1226 * data rate => double data rate; number of lanes => 4; bits per pixel => 10
1228 static u64
link_freq_to_pixel_rate(u64 f
)
1230 f
*= 2 * OV08X40_DATA_LANES
;
1236 /* Menu items for LINK_FREQ V4L2 control */
1237 static const s64 link_freq_menu_items
[] = {
1238 OV08X40_LINK_FREQ_400MHZ
,
1241 /* Link frequency configs */
1242 static const struct ov08x40_link_freq_config link_freq_configs
[] = {
1243 [OV08X40_LINK_FREQ_400MHZ_INDEX
] = {
1245 .num_of_regs
= ARRAY_SIZE(mipi_data_rate_800mbps
),
1246 .regs
= mipi_data_rate_800mbps
,
1252 static const struct ov08x40_mode supported_modes
[] = {
1256 .vts_def
= OV08X40_VTS_30FPS
,
1257 .vts_min
= OV08X40_VTS_30FPS
,
1258 .llp
= 0x10aa, /* in normal mode, tline time = 2 * HTS / SCLK */
1261 .num_of_regs
= ARRAY_SIZE(mode_3856x2416_regs
),
1262 .regs
= mode_3856x2416_regs
,
1264 .link_freq_index
= OV08X40_LINK_FREQ_400MHZ_INDEX
,
1265 .exposure_shift
= 1,
1266 .exposure_margin
= OV08X40_EXPOSURE_MAX_MARGIN
,
1271 .vts_def
= OV08X40_VTS_BIN_30FPS
,
1272 .vts_min
= OV08X40_VTS_BIN_30FPS
,
1276 .num_of_regs
= ARRAY_SIZE(mode_1928x1208_regs
),
1277 .regs
= mode_1928x1208_regs
,
1279 .link_freq_index
= OV08X40_LINK_FREQ_400MHZ_INDEX
,
1280 .exposure_shift
= 0,
1281 .exposure_margin
= OV08X40_EXPOSURE_BIN_MAX_MARGIN
,
1285 static const char * const ov08x40_supply_names
[] = {
1286 "dovdd", /* Digital I/O power */
1287 "avdd", /* Analog power */
1288 "dvdd", /* Digital core power */
1292 struct v4l2_subdev sd
;
1293 struct media_pad pad
;
1295 struct v4l2_ctrl_handler ctrl_handler
;
1297 struct v4l2_ctrl
*link_freq
;
1298 struct v4l2_ctrl
*pixel_rate
;
1299 struct v4l2_ctrl
*vblank
;
1300 struct v4l2_ctrl
*hblank
;
1301 struct v4l2_ctrl
*exposure
;
1304 struct gpio_desc
*reset_gpio
;
1305 struct regulator_bulk_data supplies
[ARRAY_SIZE(ov08x40_supply_names
)];
1308 const struct ov08x40_mode
*cur_mode
;
1310 /* Mutex for serialized access */
1313 /* True if the device has been identified */
1317 #define to_ov08x40(_sd) container_of(_sd, struct ov08x40, sd)
1319 static int ov08x40_power_on(struct device
*dev
)
1321 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
1322 struct ov08x40
*ov08x
= to_ov08x40(sd
);
1325 if (is_acpi_node(dev_fwnode(dev
)))
1328 ret
= clk_prepare_enable(ov08x
->xvclk
);
1330 dev_err(dev
, "failed to enable xvclk\n");
1334 if (ov08x
->reset_gpio
) {
1335 gpiod_set_value_cansleep(ov08x
->reset_gpio
, 1);
1336 usleep_range(1000, 2000);
1339 ret
= regulator_bulk_enable(ARRAY_SIZE(ov08x40_supply_names
),
1342 dev_err(dev
, "failed to enable regulators\n");
1346 gpiod_set_value_cansleep(ov08x
->reset_gpio
, 0);
1347 usleep_range(1500, 1800);
1352 gpiod_set_value_cansleep(ov08x
->reset_gpio
, 1);
1353 clk_disable_unprepare(ov08x
->xvclk
);
1358 static int ov08x40_power_off(struct device
*dev
)
1360 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
1361 struct ov08x40
*ov08x
= to_ov08x40(sd
);
1363 if (is_acpi_node(dev_fwnode(dev
)))
1366 gpiod_set_value_cansleep(ov08x
->reset_gpio
, 1);
1367 regulator_bulk_disable(ARRAY_SIZE(ov08x40_supply_names
),
1369 clk_disable_unprepare(ov08x
->xvclk
);
1374 /* Read registers up to 4 at a time */
1375 static int ov08x40_read_reg(struct ov08x40
*ov08x
,
1376 u16 reg
, u32 len
, u32
*val
)
1378 struct i2c_client
*client
= v4l2_get_subdevdata(&ov08x
->sd
);
1379 struct i2c_msg msgs
[2];
1383 __be16 reg_addr_be
= cpu_to_be16(reg
);
1388 data_be_p
= (u8
*)&data_be
;
1389 /* Write register address */
1390 msgs
[0].addr
= client
->addr
;
1393 msgs
[0].buf
= (u8
*)®_addr_be
;
1395 /* Read data from register */
1396 msgs
[1].addr
= client
->addr
;
1397 msgs
[1].flags
= I2C_M_RD
;
1399 msgs
[1].buf
= &data_be_p
[4 - len
];
1401 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
1402 if (ret
!= ARRAY_SIZE(msgs
))
1405 *val
= be32_to_cpu(data_be
);
1410 static int __ov08x40_burst_fill_regs(struct i2c_client
*client
, u16 first_reg
,
1411 u16 last_reg
, size_t num_regs
, u8 val
)
1413 struct i2c_msg msgs
;
1417 msgs
.addr
= client
->addr
;
1419 msgs
.len
= 2 + num_regs
;
1420 msgs
.buf
= kmalloc(msgs
.len
, GFP_KERNEL
);
1425 put_unaligned_be16(first_reg
, msgs
.buf
);
1427 for (i
= 0; i
< num_regs
; ++i
)
1428 msgs
.buf
[2 + i
] = val
;
1430 ret
= i2c_transfer(client
->adapter
, &msgs
, 1);
1435 dev_err(&client
->dev
, "Failed regs transferred: %d\n", ret
);
1442 static int ov08x40_burst_fill_regs(struct ov08x40
*ov08x
, u16 first_reg
,
1443 u16 last_reg
, u8 val
)
1445 struct i2c_client
*client
= v4l2_get_subdevdata(&ov08x
->sd
);
1446 size_t num_regs
, num_write_regs
;
1449 num_regs
= last_reg
- first_reg
+ 1;
1450 num_write_regs
= num_regs
;
1452 if (client
->adapter
->quirks
&& client
->adapter
->quirks
->max_write_len
)
1453 num_write_regs
= client
->adapter
->quirks
->max_write_len
- 2;
1455 while (first_reg
< last_reg
) {
1456 ret
= __ov08x40_burst_fill_regs(client
, first_reg
, last_reg
,
1457 num_write_regs
, val
);
1461 first_reg
+= num_write_regs
;
1467 /* Write registers up to 4 at a time */
1468 static int ov08x40_write_reg(struct ov08x40
*ov08x
,
1469 u16 reg
, u32 len
, u32 __val
)
1471 struct i2c_client
*client
= v4l2_get_subdevdata(&ov08x
->sd
);
1480 buf
[1] = reg
& 0xff;
1482 val
= cpu_to_be32(__val
);
1488 buf
[buf_i
++] = val_p
[val_i
++];
1490 if (i2c_master_send(client
, buf
, len
+ 2) != len
+ 2)
1496 /* Write a list of registers */
1497 static int ov08x40_write_regs(struct ov08x40
*ov08x
,
1498 const struct ov08x40_reg
*regs
, u32 len
)
1500 struct i2c_client
*client
= v4l2_get_subdevdata(&ov08x
->sd
);
1504 for (i
= 0; i
< len
; i
++) {
1505 ret
= ov08x40_write_reg(ov08x
, regs
[i
].address
, 1,
1509 dev_err_ratelimited(&client
->dev
,
1510 "Failed to write reg 0x%4.4x. error = %d\n",
1511 regs
[i
].address
, ret
);
1520 static int ov08x40_write_reg_list(struct ov08x40
*ov08x
,
1521 const struct ov08x40_reg_list
*r_list
)
1523 return ov08x40_write_regs(ov08x
, r_list
->regs
, r_list
->num_of_regs
);
1526 static int ov08x40_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
1528 const struct ov08x40_mode
*default_mode
= &supported_modes
[0];
1529 struct ov08x40
*ov08x
= to_ov08x40(sd
);
1530 struct v4l2_mbus_framefmt
*try_fmt
=
1531 v4l2_subdev_state_get_format(fh
->state
, 0);
1533 mutex_lock(&ov08x
->mutex
);
1535 /* Initialize try_fmt */
1536 try_fmt
->width
= default_mode
->width
;
1537 try_fmt
->height
= default_mode
->height
;
1538 try_fmt
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
1539 try_fmt
->field
= V4L2_FIELD_NONE
;
1541 /* No crop or compose */
1542 mutex_unlock(&ov08x
->mutex
);
1547 static int ov08x40_update_digital_gain(struct ov08x40
*ov08x
, u32 d_gain
)
1553 * 0x350C[1:0], 0x350B[7:0], 0x350A[4:0]
1556 val
= (d_gain
& OV08X40_DGTL_GAIN_L_MASK
) << OV08X40_DGTL_GAIN_L_SHIFT
;
1557 ret
= ov08x40_write_reg(ov08x
, OV08X40_REG_DGTL_GAIN_L
,
1558 OV08X40_REG_VALUE_08BIT
, val
);
1562 val
= (d_gain
>> OV08X40_DGTL_GAIN_M_SHIFT
) & OV08X40_DGTL_GAIN_M_MASK
;
1563 ret
= ov08x40_write_reg(ov08x
, OV08X40_REG_DGTL_GAIN_M
,
1564 OV08X40_REG_VALUE_08BIT
, val
);
1568 val
= (d_gain
>> OV08X40_DGTL_GAIN_H_SHIFT
) & OV08X40_DGTL_GAIN_H_MASK
;
1570 return ov08x40_write_reg(ov08x
, OV08X40_REG_DGTL_GAIN_H
,
1571 OV08X40_REG_VALUE_08BIT
, val
);
1574 static int ov08x40_enable_test_pattern(struct ov08x40
*ov08x
, u32 pattern
)
1579 ret
= ov08x40_read_reg(ov08x
, OV08X40_REG_TEST_PATTERN
,
1580 OV08X40_REG_VALUE_08BIT
, &val
);
1585 ret
= ov08x40_read_reg(ov08x
, OV08X40_REG_ISP
,
1586 OV08X40_REG_VALUE_08BIT
, &val
);
1590 ret
= ov08x40_write_reg(ov08x
, OV08X40_REG_ISP
,
1591 OV08X40_REG_VALUE_08BIT
,
1596 ret
= ov08x40_read_reg(ov08x
, OV08X40_REG_SHORT_TEST_PATTERN
,
1597 OV08X40_REG_VALUE_08BIT
, &val
);
1601 ret
= ov08x40_write_reg(ov08x
, OV08X40_REG_SHORT_TEST_PATTERN
,
1602 OV08X40_REG_VALUE_08BIT
,
1607 ret
= ov08x40_read_reg(ov08x
, OV08X40_REG_TEST_PATTERN
,
1608 OV08X40_REG_VALUE_08BIT
, &val
);
1612 val
&= OV08X40_TEST_PATTERN_MASK
;
1613 val
|= ((pattern
- 1) << OV08X40_TEST_PATTERN_BAR_SHIFT
) |
1614 OV08X40_TEST_PATTERN_ENABLE
;
1616 val
&= ~OV08X40_TEST_PATTERN_ENABLE
;
1619 return ov08x40_write_reg(ov08x
, OV08X40_REG_TEST_PATTERN
,
1620 OV08X40_REG_VALUE_08BIT
, val
);
1623 static int ov08x40_set_ctrl_hflip(struct ov08x40
*ov08x
, u32 ctrl_val
)
1628 ret
= ov08x40_read_reg(ov08x
, OV08X40_REG_MIRROR
,
1629 OV08X40_REG_VALUE_08BIT
, &val
);
1633 return ov08x40_write_reg(ov08x
, OV08X40_REG_MIRROR
,
1634 OV08X40_REG_VALUE_08BIT
,
1635 ctrl_val
? val
| BIT(2) : val
& ~BIT(2));
1638 static int ov08x40_set_ctrl_vflip(struct ov08x40
*ov08x
, u32 ctrl_val
)
1643 ret
= ov08x40_read_reg(ov08x
, OV08X40_REG_VFLIP
,
1644 OV08X40_REG_VALUE_08BIT
, &val
);
1648 return ov08x40_write_reg(ov08x
, OV08X40_REG_VFLIP
,
1649 OV08X40_REG_VALUE_08BIT
,
1650 ctrl_val
? val
| BIT(2) : val
& ~BIT(2));
1653 static int ov08x40_set_ctrl(struct v4l2_ctrl
*ctrl
)
1655 struct ov08x40
*ov08x
= container_of(ctrl
->handler
,
1656 struct ov08x40
, ctrl_handler
);
1657 struct i2c_client
*client
= v4l2_get_subdevdata(&ov08x
->sd
);
1663 /* Propagate change of current control to all related controls */
1665 case V4L2_CID_VBLANK
:
1666 /* Update max exposure while meeting expected vblanking */
1668 * because in normal mode, 1 HTS = 0.5 tline
1669 * fps = sclk / hts / vts
1670 * so the vts value needs to be double
1672 max
= ((ov08x
->cur_mode
->height
+ ctrl
->val
) <<
1673 ov08x
->cur_mode
->exposure_shift
) -
1674 ov08x
->cur_mode
->exposure_margin
;
1676 __v4l2_ctrl_modify_range(ov08x
->exposure
,
1677 ov08x
->exposure
->minimum
,
1678 max
, ov08x
->exposure
->step
, max
);
1683 * Applying V4L2 control value only happens
1684 * when power is up for streaming
1686 if (!pm_runtime_get_if_in_use(&client
->dev
))
1690 case V4L2_CID_ANALOGUE_GAIN
:
1691 ret
= ov08x40_write_reg(ov08x
, OV08X40_REG_ANALOG_GAIN
,
1692 OV08X40_REG_VALUE_16BIT
,
1695 case V4L2_CID_DIGITAL_GAIN
:
1696 ret
= ov08x40_update_digital_gain(ov08x
, ctrl
->val
);
1698 case V4L2_CID_EXPOSURE
:
1699 exp
= (ctrl
->val
<< ov08x
->cur_mode
->exposure_shift
) -
1700 ov08x
->cur_mode
->exposure_margin
;
1702 ret
= ov08x40_write_reg(ov08x
, OV08X40_REG_EXPOSURE
,
1703 OV08X40_REG_VALUE_24BIT
,
1706 case V4L2_CID_VBLANK
:
1707 fll
= ((ov08x
->cur_mode
->height
+ ctrl
->val
) <<
1708 ov08x
->cur_mode
->exposure_shift
);
1710 ret
= ov08x40_write_reg(ov08x
, OV08X40_REG_VTS
,
1711 OV08X40_REG_VALUE_16BIT
,
1714 case V4L2_CID_TEST_PATTERN
:
1715 ret
= ov08x40_enable_test_pattern(ov08x
, ctrl
->val
);
1717 case V4L2_CID_HFLIP
:
1718 ov08x40_set_ctrl_hflip(ov08x
, ctrl
->val
);
1720 case V4L2_CID_VFLIP
:
1721 ov08x40_set_ctrl_vflip(ov08x
, ctrl
->val
);
1724 dev_info(&client
->dev
,
1725 "ctrl(id:0x%x,val:0x%x) is not handled\n",
1726 ctrl
->id
, ctrl
->val
);
1730 pm_runtime_put(&client
->dev
);
1735 static const struct v4l2_ctrl_ops ov08x40_ctrl_ops
= {
1736 .s_ctrl
= ov08x40_set_ctrl
,
1739 static int ov08x40_enum_mbus_code(struct v4l2_subdev
*sd
,
1740 struct v4l2_subdev_state
*sd_state
,
1741 struct v4l2_subdev_mbus_code_enum
*code
)
1743 /* Only one bayer order(GRBG) is supported */
1744 if (code
->index
> 0)
1747 code
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
1752 static int ov08x40_enum_frame_size(struct v4l2_subdev
*sd
,
1753 struct v4l2_subdev_state
*sd_state
,
1754 struct v4l2_subdev_frame_size_enum
*fse
)
1756 if (fse
->index
>= ARRAY_SIZE(supported_modes
))
1759 if (fse
->code
!= MEDIA_BUS_FMT_SGRBG10_1X10
)
1762 fse
->min_width
= supported_modes
[fse
->index
].width
;
1763 fse
->max_width
= fse
->min_width
;
1764 fse
->min_height
= supported_modes
[fse
->index
].height
;
1765 fse
->max_height
= fse
->min_height
;
1770 static void ov08x40_update_pad_format(const struct ov08x40_mode
*mode
,
1771 struct v4l2_subdev_format
*fmt
)
1773 fmt
->format
.width
= mode
->width
;
1774 fmt
->format
.height
= mode
->height
;
1775 fmt
->format
.code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
1776 fmt
->format
.field
= V4L2_FIELD_NONE
;
1779 static int ov08x40_do_get_pad_format(struct ov08x40
*ov08x
,
1780 struct v4l2_subdev_state
*sd_state
,
1781 struct v4l2_subdev_format
*fmt
)
1783 struct v4l2_mbus_framefmt
*framefmt
;
1785 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1786 framefmt
= v4l2_subdev_state_get_format(sd_state
, fmt
->pad
);
1787 fmt
->format
= *framefmt
;
1789 ov08x40_update_pad_format(ov08x
->cur_mode
, fmt
);
1795 static int ov08x40_get_pad_format(struct v4l2_subdev
*sd
,
1796 struct v4l2_subdev_state
*sd_state
,
1797 struct v4l2_subdev_format
*fmt
)
1799 struct ov08x40
*ov08x
= to_ov08x40(sd
);
1802 mutex_lock(&ov08x
->mutex
);
1803 ret
= ov08x40_do_get_pad_format(ov08x
, sd_state
, fmt
);
1804 mutex_unlock(&ov08x
->mutex
);
1810 ov08x40_set_pad_format(struct v4l2_subdev
*sd
,
1811 struct v4l2_subdev_state
*sd_state
,
1812 struct v4l2_subdev_format
*fmt
)
1814 struct ov08x40
*ov08x
= to_ov08x40(sd
);
1815 const struct ov08x40_mode
*mode
;
1816 struct v4l2_mbus_framefmt
*framefmt
;
1824 mutex_lock(&ov08x
->mutex
);
1826 /* Only one raw bayer(GRBG) order is supported */
1827 if (fmt
->format
.code
!= MEDIA_BUS_FMT_SGRBG10_1X10
)
1828 fmt
->format
.code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
1830 mode
= v4l2_find_nearest_size(supported_modes
,
1831 ARRAY_SIZE(supported_modes
),
1833 fmt
->format
.width
, fmt
->format
.height
);
1834 ov08x40_update_pad_format(mode
, fmt
);
1835 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1836 framefmt
= v4l2_subdev_state_get_format(sd_state
, fmt
->pad
);
1837 *framefmt
= fmt
->format
;
1839 ov08x
->cur_mode
= mode
;
1840 __v4l2_ctrl_s_ctrl(ov08x
->link_freq
, mode
->link_freq_index
);
1841 link_freq
= link_freq_menu_items
[mode
->link_freq_index
];
1842 pixel_rate
= link_freq_to_pixel_rate(link_freq
);
1843 __v4l2_ctrl_s_ctrl_int64(ov08x
->pixel_rate
, pixel_rate
);
1845 /* Update limits and set FPS to default */
1846 vblank_def
= ov08x
->cur_mode
->vts_def
-
1847 ov08x
->cur_mode
->height
;
1848 vblank_min
= ov08x
->cur_mode
->vts_min
-
1849 ov08x
->cur_mode
->height
;
1852 * The frame length line should be aligned to a multiple of 4,
1853 * as provided by the sensor vendor, in normal mode.
1855 steps
= mode
->exposure_shift
== 1 ? 4 : 1;
1857 __v4l2_ctrl_modify_range(ov08x
->vblank
, vblank_min
,
1859 - ov08x
->cur_mode
->height
,
1862 __v4l2_ctrl_s_ctrl(ov08x
->vblank
, vblank_def
);
1864 h_blank
= ov08x
->cur_mode
->llp
- ov08x
->cur_mode
->width
;
1866 __v4l2_ctrl_modify_range(ov08x
->hblank
, h_blank
,
1867 h_blank
, 1, h_blank
);
1870 mutex_unlock(&ov08x
->mutex
);
1875 static int ov08x40_start_streaming(struct ov08x40
*ov08x
)
1877 struct i2c_client
*client
= v4l2_get_subdevdata(&ov08x
->sd
);
1878 const struct ov08x40_reg_list
*reg_list
;
1879 int ret
, link_freq_index
;
1881 /* Get out of from software reset */
1882 ret
= ov08x40_write_reg(ov08x
, OV08X40_REG_SOFTWARE_RST
,
1883 OV08X40_REG_VALUE_08BIT
, OV08X40_SOFTWARE_RST
);
1885 dev_err(&client
->dev
, "%s failed to set powerup registers\n",
1890 link_freq_index
= ov08x
->cur_mode
->link_freq_index
;
1891 reg_list
= &link_freq_configs
[link_freq_index
].reg_list
;
1893 ret
= ov08x40_write_reg_list(ov08x
, reg_list
);
1895 dev_err(&client
->dev
, "%s failed to set plls\n", __func__
);
1899 /* Apply default values of current mode */
1900 reg_list
= &ov08x
->cur_mode
->reg_list
;
1901 ret
= ov08x40_write_reg_list(ov08x
, reg_list
);
1903 dev_err(&client
->dev
, "%s failed to set mode\n", __func__
);
1907 /* Use i2c burst to write register on full size registers */
1908 if (ov08x
->cur_mode
->exposure_shift
== 1) {
1909 ret
= ov08x40_burst_fill_regs(ov08x
, OV08X40_REG_XTALK_FIRST_A
,
1910 OV08X40_REG_XTALK_LAST_A
, 0x75);
1912 ret
= ov08x40_burst_fill_regs(ov08x
,
1913 OV08X40_REG_XTALK_FIRST_B
,
1914 OV08X40_REG_XTALK_LAST_B
,
1919 dev_err(&client
->dev
, "%s failed to set regs\n", __func__
);
1923 /* Apply customized values from user */
1924 ret
= __v4l2_ctrl_handler_setup(ov08x
->sd
.ctrl_handler
);
1928 return ov08x40_write_reg(ov08x
, OV08X40_REG_MODE_SELECT
,
1929 OV08X40_REG_VALUE_08BIT
,
1930 OV08X40_MODE_STREAMING
);
1933 /* Stop streaming */
1934 static int ov08x40_stop_streaming(struct ov08x40
*ov08x
)
1936 return ov08x40_write_reg(ov08x
, OV08X40_REG_MODE_SELECT
,
1937 OV08X40_REG_VALUE_08BIT
, OV08X40_MODE_STANDBY
);
1940 static int ov08x40_set_stream(struct v4l2_subdev
*sd
, int enable
)
1942 struct ov08x40
*ov08x
= to_ov08x40(sd
);
1943 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1946 mutex_lock(&ov08x
->mutex
);
1949 ret
= pm_runtime_resume_and_get(&client
->dev
);
1954 * Apply default & customized values
1955 * and then start streaming.
1957 ret
= ov08x40_start_streaming(ov08x
);
1961 ov08x40_stop_streaming(ov08x
);
1962 pm_runtime_put(&client
->dev
);
1965 mutex_unlock(&ov08x
->mutex
);
1970 pm_runtime_put(&client
->dev
);
1972 mutex_unlock(&ov08x
->mutex
);
1977 /* Verify chip ID */
1978 static int ov08x40_identify_module(struct ov08x40
*ov08x
)
1980 struct i2c_client
*client
= v4l2_get_subdevdata(&ov08x
->sd
);
1984 if (ov08x
->identified
)
1987 ret
= ov08x40_read_reg(ov08x
, OV08X40_REG_CHIP_ID
,
1988 OV08X40_REG_VALUE_24BIT
, &val
);
1992 if (val
!= OV08X40_CHIP_ID
) {
1993 dev_err(&client
->dev
, "chip id mismatch: %x!=%x\n",
1994 OV08X40_CHIP_ID
, val
);
1998 ov08x
->identified
= true;
2003 static const struct v4l2_subdev_video_ops ov08x40_video_ops
= {
2004 .s_stream
= ov08x40_set_stream
,
2007 static const struct v4l2_subdev_pad_ops ov08x40_pad_ops
= {
2008 .enum_mbus_code
= ov08x40_enum_mbus_code
,
2009 .get_fmt
= ov08x40_get_pad_format
,
2010 .set_fmt
= ov08x40_set_pad_format
,
2011 .enum_frame_size
= ov08x40_enum_frame_size
,
2014 static const struct v4l2_subdev_ops ov08x40_subdev_ops
= {
2015 .video
= &ov08x40_video_ops
,
2016 .pad
= &ov08x40_pad_ops
,
2019 static const struct media_entity_operations ov08x40_subdev_entity_ops
= {
2020 .link_validate
= v4l2_subdev_link_validate
,
2023 static const struct v4l2_subdev_internal_ops ov08x40_internal_ops
= {
2024 .open
= ov08x40_open
,
2027 static int ov08x40_init_controls(struct ov08x40
*ov08x
)
2029 struct i2c_client
*client
= v4l2_get_subdevdata(&ov08x
->sd
);
2030 struct v4l2_fwnode_device_properties props
;
2031 struct v4l2_ctrl_handler
*ctrl_hdlr
;
2038 const struct ov08x40_mode
*mode
;
2042 ctrl_hdlr
= &ov08x
->ctrl_handler
;
2043 ret
= v4l2_ctrl_handler_init(ctrl_hdlr
, 10);
2047 mutex_init(&ov08x
->mutex
);
2048 ctrl_hdlr
->lock
= &ov08x
->mutex
;
2049 max
= ARRAY_SIZE(link_freq_menu_items
) - 1;
2050 ov08x
->link_freq
= v4l2_ctrl_new_int_menu(ctrl_hdlr
,
2055 link_freq_menu_items
);
2056 if (ov08x
->link_freq
)
2057 ov08x
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
2059 pixel_rate_max
= link_freq_to_pixel_rate(link_freq_menu_items
[0]);
2061 /* By default, PIXEL_RATE is read only */
2062 ov08x
->pixel_rate
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov08x40_ctrl_ops
,
2063 V4L2_CID_PIXEL_RATE
,
2064 pixel_rate_min
, pixel_rate_max
,
2067 mode
= ov08x
->cur_mode
;
2068 vblank_def
= mode
->vts_def
- mode
->height
;
2069 vblank_min
= mode
->vts_min
- mode
->height
;
2070 ov08x
->vblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov08x40_ctrl_ops
,
2073 OV08X40_VTS_MAX
- mode
->height
, 1,
2076 hblank
= ov08x
->cur_mode
->llp
- ov08x
->cur_mode
->width
;
2078 ov08x
->hblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov08x40_ctrl_ops
,
2080 hblank
, hblank
, 1, hblank
);
2082 ov08x
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
2084 exposure_max
= mode
->vts_def
- OV08X40_EXPOSURE_MAX_MARGIN
;
2085 ov08x
->exposure
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov08x40_ctrl_ops
,
2087 OV08X40_EXPOSURE_MIN
,
2088 exposure_max
, OV08X40_EXPOSURE_STEP
,
2091 v4l2_ctrl_new_std(ctrl_hdlr
, &ov08x40_ctrl_ops
, V4L2_CID_ANALOGUE_GAIN
,
2092 OV08X40_ANA_GAIN_MIN
, OV08X40_ANA_GAIN_MAX
,
2093 OV08X40_ANA_GAIN_STEP
, OV08X40_ANA_GAIN_DEFAULT
);
2096 v4l2_ctrl_new_std(ctrl_hdlr
, &ov08x40_ctrl_ops
, V4L2_CID_DIGITAL_GAIN
,
2097 OV08X40_DGTL_GAIN_MIN
, OV08X40_DGTL_GAIN_MAX
,
2098 OV08X40_DGTL_GAIN_STEP
, OV08X40_DGTL_GAIN_DEFAULT
);
2100 v4l2_ctrl_new_std_menu_items(ctrl_hdlr
, &ov08x40_ctrl_ops
,
2101 V4L2_CID_TEST_PATTERN
,
2102 ARRAY_SIZE(ov08x40_test_pattern_menu
) - 1,
2103 0, 0, ov08x40_test_pattern_menu
);
2105 v4l2_ctrl_new_std(ctrl_hdlr
, &ov08x40_ctrl_ops
,
2106 V4L2_CID_HFLIP
, 0, 1, 1, 0);
2107 v4l2_ctrl_new_std(ctrl_hdlr
, &ov08x40_ctrl_ops
,
2108 V4L2_CID_VFLIP
, 0, 1, 1, 0);
2110 if (ctrl_hdlr
->error
) {
2111 ret
= ctrl_hdlr
->error
;
2112 dev_err(&client
->dev
, "%s control init failed (%d)\n",
2117 ret
= v4l2_fwnode_device_parse(&client
->dev
, &props
);
2121 ret
= v4l2_ctrl_new_fwnode_properties(ctrl_hdlr
, &ov08x40_ctrl_ops
,
2126 ov08x
->sd
.ctrl_handler
= ctrl_hdlr
;
2131 v4l2_ctrl_handler_free(ctrl_hdlr
);
2132 mutex_destroy(&ov08x
->mutex
);
2137 static void ov08x40_free_controls(struct ov08x40
*ov08x
)
2139 v4l2_ctrl_handler_free(ov08x
->sd
.ctrl_handler
);
2140 mutex_destroy(&ov08x
->mutex
);
2143 static int ov08x40_check_hwcfg(struct ov08x40
*ov08x
, struct device
*dev
)
2145 struct v4l2_fwnode_endpoint bus_cfg
= {
2146 .bus_type
= V4L2_MBUS_CSI2_DPHY
2148 struct fwnode_handle
*ep
;
2149 struct fwnode_handle
*fwnode
= dev_fwnode(dev
);
2157 if (!is_acpi_node(fwnode
)) {
2158 ov08x
->xvclk
= devm_clk_get(dev
, NULL
);
2159 if (IS_ERR(ov08x
->xvclk
)) {
2160 dev_err(dev
, "could not get xvclk clock (%pe)\n",
2162 return PTR_ERR(ov08x
->xvclk
);
2165 xvclk_rate
= clk_get_rate(ov08x
->xvclk
);
2167 ov08x
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset",
2169 if (IS_ERR(ov08x
->reset_gpio
))
2170 return PTR_ERR(ov08x
->reset_gpio
);
2172 for (i
= 0; i
< ARRAY_SIZE(ov08x40_supply_names
); i
++)
2173 ov08x
->supplies
[i
].supply
= ov08x40_supply_names
[i
];
2175 ret
= devm_regulator_bulk_get(dev
,
2176 ARRAY_SIZE(ov08x40_supply_names
),
2181 ret
= fwnode_property_read_u32(dev_fwnode(dev
), "clock-frequency",
2184 dev_err(dev
, "can't get clock frequency");
2189 if (xvclk_rate
!= OV08X40_XVCLK
) {
2190 dev_err(dev
, "external clock %d is not supported",
2195 ep
= fwnode_graph_get_next_endpoint(fwnode
, NULL
);
2199 ret
= v4l2_fwnode_endpoint_alloc_parse(ep
, &bus_cfg
);
2200 fwnode_handle_put(ep
);
2204 if (bus_cfg
.bus
.mipi_csi2
.num_data_lanes
!= OV08X40_DATA_LANES
) {
2205 dev_err(dev
, "number of CSI2 data lanes %d is not supported",
2206 bus_cfg
.bus
.mipi_csi2
.num_data_lanes
);
2211 if (!bus_cfg
.nr_of_link_frequencies
) {
2212 dev_err(dev
, "no link frequencies defined");
2217 for (i
= 0; i
< ARRAY_SIZE(link_freq_menu_items
); i
++) {
2218 for (j
= 0; j
< bus_cfg
.nr_of_link_frequencies
; j
++) {
2219 if (link_freq_menu_items
[i
] ==
2220 bus_cfg
.link_frequencies
[j
])
2224 if (j
== bus_cfg
.nr_of_link_frequencies
) {
2225 dev_err(dev
, "no link frequency %lld supported",
2226 link_freq_menu_items
[i
]);
2233 v4l2_fwnode_endpoint_free(&bus_cfg
);
2238 static int ov08x40_probe(struct i2c_client
*client
)
2239 { struct ov08x40
*ov08x
;
2243 ov08x
= devm_kzalloc(&client
->dev
, sizeof(*ov08x
), GFP_KERNEL
);
2247 /* Check HW config */
2248 ret
= ov08x40_check_hwcfg(ov08x
, &client
->dev
);
2250 dev_err(&client
->dev
, "failed to check hwcfg: %d", ret
);
2254 /* Initialize subdev */
2255 v4l2_i2c_subdev_init(&ov08x
->sd
, client
, &ov08x40_subdev_ops
);
2257 full_power
= acpi_dev_state_d0(&client
->dev
);
2259 ret
= ov08x40_power_on(&client
->dev
);
2261 dev_err(&client
->dev
, "failed to power on\n");
2265 /* Check module identity */
2266 ret
= ov08x40_identify_module(ov08x
);
2268 dev_err(&client
->dev
, "failed to find sensor: %d\n", ret
);
2269 goto probe_power_off
;
2273 /* Set default mode to max resolution */
2274 ov08x
->cur_mode
= &supported_modes
[0];
2276 ret
= ov08x40_init_controls(ov08x
);
2278 goto probe_power_off
;
2280 /* Initialize subdev */
2281 ov08x
->sd
.internal_ops
= &ov08x40_internal_ops
;
2282 ov08x
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
2283 ov08x
->sd
.entity
.ops
= &ov08x40_subdev_entity_ops
;
2284 ov08x
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
2286 /* Initialize source pad */
2287 ov08x
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
2288 ret
= media_entity_pads_init(&ov08x
->sd
.entity
, 1, &ov08x
->pad
);
2290 dev_err(&client
->dev
, "%s failed:%d\n", __func__
, ret
);
2291 goto error_handler_free
;
2294 ret
= v4l2_async_register_subdev_sensor(&ov08x
->sd
);
2296 goto error_media_entity
;
2299 pm_runtime_set_active(&client
->dev
);
2300 pm_runtime_enable(&client
->dev
);
2301 pm_runtime_idle(&client
->dev
);
2306 media_entity_cleanup(&ov08x
->sd
.entity
);
2309 ov08x40_free_controls(ov08x
);
2312 ov08x40_power_off(&client
->dev
);
2317 static void ov08x40_remove(struct i2c_client
*client
)
2319 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
2320 struct ov08x40
*ov08x
= to_ov08x40(sd
);
2322 v4l2_async_unregister_subdev(sd
);
2323 media_entity_cleanup(&sd
->entity
);
2324 ov08x40_free_controls(ov08x
);
2326 pm_runtime_disable(&client
->dev
);
2327 pm_runtime_set_suspended(&client
->dev
);
2329 ov08x40_power_off(&client
->dev
);
2333 static const struct acpi_device_id ov08x40_acpi_ids
[] = {
2338 MODULE_DEVICE_TABLE(acpi
, ov08x40_acpi_ids
);
2341 static const struct of_device_id ov08x40_of_match
[] = {
2342 { .compatible
= "ovti,ov08x40" },
2345 MODULE_DEVICE_TABLE(of
, ov08x40_of_match
);
2347 static struct i2c_driver ov08x40_i2c_driver
= {
2350 .acpi_match_table
= ACPI_PTR(ov08x40_acpi_ids
),
2351 .of_match_table
= ov08x40_of_match
,
2353 .probe
= ov08x40_probe
,
2354 .remove
= ov08x40_remove
,
2355 .flags
= I2C_DRV_ACPI_WAIVE_D0_PROBE
,
2358 module_i2c_driver(ov08x40_i2c_driver
);
2360 MODULE_AUTHOR("Jason Chen <jason.z.chen@intel.com>");
2361 MODULE_AUTHOR("Qingwu Zhang <qingwu.zhang@intel.com>");
2362 MODULE_AUTHOR("Shawn Tu");
2363 MODULE_DESCRIPTION("OmniVision OV08X40 sensor driver");
2364 MODULE_LICENSE("GPL");