diff --git a/drivers/media/i2c/ov9734.c b/drivers/media/i2c/ov9734.c index 0eaf33807fc92..8876c8db4a6fd 100644 --- a/drivers/media/i2c/ov9734.c +++ b/drivers/media/i2c/ov9734.c @@ -4,9 +4,11 @@ #include #include #include +#include #include #include #include +#include #include #include @@ -27,6 +29,14 @@ #define OV9734_MODE_STANDBY 0x00 #define OV9734_MODE_STREAMING 0x01 +static const char * const ov9734_supply_names[] = { + "avdd", /* Analog power */ + "dovdd", /* Digital I/O power */ + "dvdd", /* Digital core power */ +}; + +#define OV9734_NUM_SUPPLIES ARRAY_SIZE(ov9734_supply_names) + /* vertical-timings from sensor */ #define OV9734_REG_VTS 0x380e #define OV9734_VTS_30FPS 0x0322 @@ -325,6 +335,8 @@ static const struct ov9734_mode supported_modes[] = { struct ov9734 { struct device *dev; struct clk *clk; + struct gpio_desc *reset; + struct regulator_bulk_data supplies[OV9734_NUM_SUPPLIES]; struct v4l2_subdev sd; struct media_pad pad; @@ -805,6 +817,45 @@ static const struct v4l2_subdev_internal_ops ov9734_internal_ops = { .open = ov9734_open, }; +static int ov9734_power_off(struct device *dev) +{ + struct v4l2_subdev *sd = dev_get_drvdata(dev); + struct ov9734 *ov9734 = to_ov9734(sd); + + gpiod_set_value_cansleep(ov9734->reset, 1); + clk_disable_unprepare(ov9734->clk); + regulator_bulk_disable(OV9734_NUM_SUPPLIES, ov9734->supplies); + + return 0; +} + +static int ov9734_power_on(struct device *dev) +{ + struct v4l2_subdev *sd = dev_get_drvdata(dev); + struct ov9734 *ov9734 = to_ov9734(sd); + int ret; + + ret = regulator_bulk_enable(OV9734_NUM_SUPPLIES, ov9734->supplies); + if (ret) { + dev_err(dev, "failed to enable regulators: %d", ret); + return ret; + } + + ret = clk_prepare_enable(ov9734->clk); + if (ret) { + dev_err(dev, "failed to enable clock: %d", ret); + regulator_bulk_disable(OV9734_NUM_SUPPLIES, ov9734->supplies); + return ret; + } + + usleep_range(1000, 2000); + gpiod_set_value_cansleep(ov9734->reset, 0); + /* 8192 xvclk cycles before the first SCCB transaction, with margin */ + usleep_range(5000, 6000); + + return 0; +} + static int ov9734_identify_module(struct ov9734 *ov9734) { int ret; @@ -836,9 +887,13 @@ static int ov9734_check_hwcfg(struct device *dev) if (!fwnode) return -ENXIO; + /* + * The fwnode graph may be created by a bridge driver such as + * ipu-bridge, which can probe after the sensor. Wait for it. + */ ep = fwnode_graph_get_next_endpoint(fwnode, NULL); if (!ep) - return -ENXIO; + return -EPROBE_DEFER; ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg); fwnode_handle_put(ep); @@ -881,6 +936,8 @@ static void ov9734_remove(struct i2c_client *client) media_entity_cleanup(&sd->entity); v4l2_ctrl_handler_free(sd->ctrl_handler); pm_runtime_disable(ov9734->dev); + if (!pm_runtime_status_suspended(ov9734->dev)) + ov9734_power_off(ov9734->dev); pm_runtime_set_suspended(ov9734->dev); mutex_destroy(&ov9734->mutex); } @@ -892,11 +949,9 @@ static int ov9734_probe(struct i2c_client *client) int ret; ret = ov9734_check_hwcfg(&client->dev); - if (ret) { - dev_err(&client->dev, "failed to check HW configuration: %d", - ret); - return ret; - } + if (ret) + return dev_err_probe(&client->dev, ret, + "failed to check HW configuration\n"); ov9734 = devm_kzalloc(&client->dev, sizeof(*ov9734), GFP_KERNEL); if (!ov9734) @@ -915,11 +970,31 @@ static int ov9734_probe(struct i2c_client *client) "external clock %lu is not supported", freq); + ov9734->reset = devm_gpiod_get_optional(ov9734->dev, "reset", + GPIOD_OUT_HIGH); + if (IS_ERR(ov9734->reset)) + return dev_err_probe(ov9734->dev, PTR_ERR(ov9734->reset), + "failed to get reset gpio\n"); + + for (unsigned int i = 0; i < OV9734_NUM_SUPPLIES; i++) + ov9734->supplies[i].supply = ov9734_supply_names[i]; + + ret = devm_regulator_bulk_get(ov9734->dev, OV9734_NUM_SUPPLIES, + ov9734->supplies); + if (ret) + return dev_err_probe(ov9734->dev, ret, + "failed to get regulators\n"); + v4l2_i2c_subdev_init(&ov9734->sd, client, &ov9734_subdev_ops); + + ret = ov9734_power_on(ov9734->dev); + if (ret) + return ret; + ret = ov9734_identify_module(ov9734); if (ret) { dev_err(ov9734->dev, "failed to find sensor: %d", ret); - return ret; + goto probe_error_power_off; } mutex_init(&ov9734->mutex); @@ -941,10 +1016,7 @@ static int ov9734_probe(struct i2c_client *client) goto probe_error_v4l2_ctrl_handler_free; } - /* - * Device is already turned on by i2c-core with ACPI domain PM. - * Enable runtime PM and turn off the device. - */ + /* Device is powered on above. Enable runtime PM and turn it off. */ pm_runtime_set_active(ov9734->dev); pm_runtime_enable(ov9734->dev); pm_runtime_idle(ov9734->dev); @@ -960,16 +1032,34 @@ static int ov9734_probe(struct i2c_client *client) probe_error_media_entity_cleanup_pm: pm_runtime_disable(ov9734->dev); + if (!pm_runtime_status_suspended(ov9734->dev)) + ov9734_power_off(ov9734->dev); pm_runtime_set_suspended(ov9734->dev); media_entity_cleanup(&ov9734->sd.entity); + v4l2_ctrl_handler_free(ov9734->sd.ctrl_handler); + mutex_destroy(&ov9734->mutex); + + return ret; probe_error_v4l2_ctrl_handler_free: v4l2_ctrl_handler_free(ov9734->sd.ctrl_handler); mutex_destroy(&ov9734->mutex); +probe_error_power_off: + ov9734_power_off(ov9734->dev); + return ret; } +/* + * Runtime PM callbacks only: ipu3-cio2 stops and restarts the sensor across + * system sleep through .s_stream(), so system sleep callbacks here would race + * with it. + */ +static const struct dev_pm_ops ov9734_pm_ops = { + RUNTIME_PM_OPS(ov9734_power_off, ov9734_power_on, NULL) +}; + static const struct acpi_device_id ov9734_acpi_ids[] = { { "OVTI9734", }, {} @@ -981,6 +1071,7 @@ static struct i2c_driver ov9734_i2c_driver = { .driver = { .name = "ov9734", .acpi_match_table = ov9734_acpi_ids, + .pm = pm_ptr(&ov9734_pm_ops), }, .probe = ov9734_probe, .remove = ov9734_remove, diff --git a/drivers/media/pci/intel/ipu-bridge.c b/drivers/media/pci/intel/ipu-bridge.c index 271b987660c5c..10a1a6e52acbd 100644 --- a/drivers/media/pci/intel/ipu-bridge.c +++ b/drivers/media/pci/intel/ipu-bridge.c @@ -101,6 +101,8 @@ static const struct ipu_sensor_config ipu_supported_sensors[] = { IPU_SENSOR_CONFIG("OVTI5675", 1, 450000000), /* Omnivision OV8856 */ IPU_SENSOR_CONFIG("OVTI8856", 3, 180000000, 360000000, 720000000), + /* Omnivision OV9734 - Surface Laptop 1/2 front camera */ + IPU_SENSOR_CONFIG("OVTI9734", 1, 180000000), /* Sony IMX471 */ IPU_SENSOR_CONFIG("SONY471A", 1, 200000000), /* Sony IMX471 (found on Lenovo X1 Carbon G14) */