Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

media: v4l2-common: Add legacy camera sensor clock helper

The recently introduced devm_v4l2_sensor_clk_get() helper aims at
simplifying sensor drivers by centralizing clock handling code, as well
as reducing cargo-cult and deprecated behaviour.

A set of drivers implement external clock handling in a non-standard
way. This can't be changed as there is a high risk of breaking existing
platforms, but keeping the code as-is creates a risk of new drivers
copying deprecated behaviour.

To fix this, introduce a new devm_v4l2_sensor_clk_get_legacy() helper
and use it in those driver. Compared to devm_v4l2_sensor_clk_get(), the
new helper takes the "clock-frequency" property into account and sets
the external clock rate on OF platforms, and adds the ability to specify
a fixed default or fallback clock rate in case the "clock-frequency"
property is not present.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund+renesas@ragnatech.se>
Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Reviewed-by: Mehdi Djait <mehdi.djait@linux.intel.com>
Signed-off-by: Hans Verkuil <hverkuil+cisco@kernel.org>

authored by

Laurent Pinchart and committed by
Mauro Carvalho Chehab
dd235b07 bfa62630

+70 -10
+30 -9
drivers/media/v4l2-core/v4l2-common.c
··· 708 708 } 709 709 EXPORT_SYMBOL_GPL(v4l2_link_freq_to_bitmap); 710 710 711 - struct clk *devm_v4l2_sensor_clk_get(struct device *dev, const char *id) 711 + struct clk *__devm_v4l2_sensor_clk_get(struct device *dev, const char *id, 712 + bool legacy, bool fixed_rate, 713 + unsigned long clk_rate) 712 714 { 715 + bool of_node = is_of_node(dev_fwnode(dev)); 713 716 const char *clk_id __free(kfree) = NULL; 714 717 struct clk_hw *clk_hw; 715 718 struct clk *clk; 716 - bool of_node; 717 - u32 rate; 718 - int ret; 719 + u32 rate = clk_rate; 720 + int ret = 0; 719 721 720 722 clk = devm_clk_get_optional(dev, id); 721 723 if (IS_ERR(clk)) 722 724 return clk; 723 725 724 - ret = device_property_read_u32(dev, "clock-frequency", &rate); 725 - of_node = is_of_node(dev_fwnode(dev)); 726 + /* 727 + * If the caller didn't request a fixed rate, retrieve it from the 728 + * clock-frequency property. -EINVAL indicates the property is absent, 729 + * and is not a failure. Other errors, or success with a clock-frequency 730 + * value of 0, are hard failures. 731 + */ 732 + if (!fixed_rate || !clk_rate) { 733 + ret = device_property_read_u32(dev, "clock-frequency", &rate); 734 + if ((ret && ret != -EINVAL) || (!ret && !rate)) 735 + return ERR_PTR(-EINVAL); 736 + } 726 737 727 738 if (clk) { 728 - if (!ret && !of_node) { 739 + /* 740 + * On non-OF platforms, or when legacy behaviour is requested, 741 + * set the clock rate if a rate has been specified by the caller 742 + * or by the clock-frequency property. 743 + */ 744 + if (rate && (!of_node || legacy)) { 729 745 ret = clk_set_rate(clk, rate); 730 746 if (ret) { 731 747 dev_err(dev, "Failed to set clock rate: %u\n", ··· 752 736 return clk; 753 737 } 754 738 755 - if (!IS_ENABLED(CONFIG_COMMON_CLK) || of_node) 739 + /* 740 + * Register a dummy fixed clock on non-OF platforms or when legacy 741 + * behaviour is requested. This required the common clock framework. 742 + */ 743 + if (!IS_ENABLED(CONFIG_COMMON_CLK) || (of_node && !legacy)) 756 744 return ERR_PTR(-ENOENT); 757 745 746 + /* We need a rate to create a clock. */ 758 747 if (ret) 759 748 return ERR_PTR(ret == -EINVAL ? -EPROBE_DEFER : ret); 760 749 ··· 776 755 777 756 return clk_hw->clk; 778 757 } 779 - EXPORT_SYMBOL_GPL(devm_v4l2_sensor_clk_get); 758 + EXPORT_SYMBOL_GPL(__devm_v4l2_sensor_clk_get);
+40 -1
include/media/v4l2-common.h
··· 612 612 unsigned int num_of_driver_link_freqs, 613 613 unsigned long *bitmap); 614 614 615 + struct clk *__devm_v4l2_sensor_clk_get(struct device *dev, const char *id, 616 + bool legacy, bool fixed_rate, 617 + unsigned long clk_rate); 618 + 615 619 /** 616 620 * devm_v4l2_sensor_clk_get - lookup and obtain a reference to a clock producer 617 621 * for a camera sensor ··· 648 644 * 649 645 * Returns a pointer to a struct clk on success or an error pointer on failure. 650 646 */ 651 - struct clk *devm_v4l2_sensor_clk_get(struct device *dev, const char *id); 647 + static inline struct clk * 648 + devm_v4l2_sensor_clk_get(struct device *dev, const char *id) 649 + { 650 + return __devm_v4l2_sensor_clk_get(dev, id, false, false, 0); 651 + } 652 + 653 + /** 654 + * devm_v4l2_sensor_clk_get_legacy - lookup and obtain a reference to a clock 655 + * producer for a camera sensor. 656 + * 657 + * @dev: device for v4l2 sensor clock "consumer" 658 + * @id: clock consumer ID 659 + * @fixed_rate: interpret the @clk_rate as a fixed rate or default rate 660 + * @clk_rate: the clock rate 661 + * 662 + * This function behaves the same way as devm_v4l2_sensor_clk_get() except that 663 + * it extends the behaviour on ACPI platforms to all platforms. 664 + * 665 + * The function also provides the ability to set the clock rate to a fixed 666 + * frequency by setting @fixed_rate to true and specifying the fixed frequency 667 + * in @clk_rate, or to use a default clock rate when the "clock-frequency" 668 + * property is absent by setting @fixed_rate to false and specifying the default 669 + * frequency in @clk_rate. Setting @fixed_rate to true and @clk_rate to 0 is an 670 + * error. 671 + * 672 + * This function is meant to support legacy behaviour in existing drivers only. 673 + * It must not be used in any new driver. 674 + * 675 + * Returns a pointer to a struct clk on success or an error pointer on failure. 676 + */ 677 + static inline struct clk * 678 + devm_v4l2_sensor_clk_get_legacy(struct device *dev, const char *id, 679 + bool fixed_rate, unsigned long clk_rate) 680 + { 681 + return __devm_v4l2_sensor_clk_get(dev, id, true, fixed_rate, clk_rate); 682 + } 652 683 653 684 static inline u64 v4l2_buffer_get_timestamp(const struct v4l2_buffer *buf) 654 685 {