[PATCH 00/15] rtc: convert from clk round_rate() to determine_rate() and fix a few bugs

Brian Masney posted 15 patches 2 months, 4 weeks ago
drivers/rtc/rtc-ds1307.c   | 15 ++++++++++-----
drivers/rtc/rtc-hym8563.c  | 15 ++++++++++-----
drivers/rtc/rtc-m41t80.c   | 21 +++++++++++----------
drivers/rtc/rtc-max31335.c | 12 +++++++-----
drivers/rtc/rtc-nct3018y.c | 15 ++++++++++-----
drivers/rtc/rtc-pcf85063.c | 15 ++++++++++-----
drivers/rtc/rtc-pcf8563.c  | 15 ++++++++++-----
drivers/rtc/rtc-rv3028.c   | 15 ++++++++++-----
drivers/rtc/rtc-rv3032.c   | 21 +++++++++++++--------
9 files changed, 91 insertions(+), 53 deletions(-)
[PATCH 00/15] rtc: convert from clk round_rate() to determine_rate() and fix a few bugs
Posted by Brian Masney 2 months, 4 weeks ago
The round_rate() clk ops is deprecated in the clk framework in favor
of the determine_rate() clk ops, so let's go ahead and convert the
drivers in the rtc subsystem using the Coccinelle semantic patch
posted below. I did a few minor cosmetic cleanups of the code in a
few cases.

I also noticed that in some of the drivers that if round_rate() is
called with a requested rate higher than the highest supported rate,
then the clock is disabled. According to the clk API, round_rate()
should instead return the highest supported rate. This series also
updates the functions to return the maximum supported rate.

Coccinelle semantic patch:

    virtual patch

    // Look up the current name of the round_rate function
    @ has_round_rate @
    identifier round_rate_name =~ ".*_round_rate";
    identifier hw_param, rate_param, parent_rate_param;
    @@

    long round_rate_name(struct clk_hw *hw_param, unsigned long rate_param,
                  unsigned long *parent_rate_param)
    {
    	...
    }

    // Rename the route_rate function name to determine_rate()
    @ script:python generate_name depends on has_round_rate @
    round_rate_name << has_round_rate.round_rate_name;
    new_name;
    @@

    coccinelle.new_name = round_rate_name.replace("_round_rate", "_determine_rate")

    // Change rate to req->rate; also change occurrences of 'return XXX'.
    @ chg_rate depends on generate_name @
    identifier has_round_rate.round_rate_name;
    identifier has_round_rate.hw_param;
    identifier has_round_rate.rate_param;
    identifier has_round_rate.parent_rate_param;
    identifier ERR =~ "E.*";
    expression E;
    @@

    long round_rate_name(struct clk_hw *hw_param, unsigned long rate_param,
                  unsigned long *parent_rate_param)
    {
    <...
    (
    -return -ERR;
    +return -ERR;
    |
    - return rate_param;
    + return 0;
    |
    - return E;
    + req->rate = E;
    +
    + return 0;
    |
    - rate_param
    + req->rate
    )
    ...>
    }

    // Coccinelle only transforms the first occurrence of the rate parameter
    // Run a second time. FIXME: Is there a better way to do this?
    @ chg_rate2 depends on generate_name @
    identifier has_round_rate.round_rate_name;
    identifier has_round_rate.hw_param;
    identifier has_round_rate.rate_param;
    identifier has_round_rate.parent_rate_param;
    @@

    long round_rate_name(struct clk_hw *hw_param, unsigned long rate_param,
                  unsigned long *parent_rate_param)
    {
    <...
    - rate_param
    + req->rate
    ...>
    }

    // Change parent_rate to req->best_parent_rate
    @ chg_parent_rate depends on generate_name @
    identifier has_round_rate.round_rate_name;
    identifier has_round_rate.hw_param;
    identifier has_round_rate.rate_param;
    identifier has_round_rate.parent_rate_param;
    @@

    long round_rate_name(struct clk_hw *hw_param, unsigned long rate_param,
                  unsigned long *parent_rate_param)
    {
    <...
    (
    - *parent_rate_param
    + req->best_parent_rate
    |
    - parent_rate_param
    + &req->best_parent_rate
    )
    ...>
    }

    // Convert the function definition from round_rate() to determine_rate()
    @ func_definition depends on chg_rate @
    identifier has_round_rate.round_rate_name;
    identifier has_round_rate.hw_param;
    identifier has_round_rate.rate_param;
    identifier has_round_rate.parent_rate_param;
    identifier generate_name.new_name;
    @@

    - long round_rate_name(struct clk_hw *hw_param, unsigned long rate_param,
    -               unsigned long *parent_rate_param)
    + int new_name(struct clk_hw *hw, struct clk_rate_request *req)
    {
        ...
    }

    // Update the ops from round_rate() to determine_rate()
    @ ops depends on func_definition @
    identifier has_round_rate.round_rate_name;
    identifier generate_name.new_name;
    @@

    {
        ...,
    -   .round_rate = round_rate_name,
    +   .determine_rate = new_name,
        ...,
    }

Note that I used coccinelle 1.2 instead of 1.3 since the newer version
adds unnecessary braces as described in this post.
https://lore.kernel.org/cocci/67642477-5f3e-4b2a-914d-579a54f48cbd@intel.com/

Signed-off-by: Brian Masney <bmasney@redhat.com>
---
Brian Masney (15):
      rtc: ds1307: fix incorrect maximum clock rate handling
      rtc: hym8563: fix incorrect maximum clock rate handling
      rtc: nct3018y: fix incorrect maximum clock rate handling
      rtc: pcf85063: fix incorrect maximum clock rate handling
      rtc: pcf8563: fix incorrect maximum clock rate handling
      rtc: rv3028: fix incorrect maximum clock rate handling
      rtc: ds1307: convert from round_rate() to determine_rate()
      rtc: hym8563: convert from round_rate() to determine_rate()
      rtc: m41t80: convert from round_rate() to determine_rate()
      rtc: max31335: convert from round_rate() to determine_rate()
      rtc: nct3018y: convert from round_rate() to determine_rate()
      rtc: pcf85063: convert from round_rate() to determine_rate()
      rtc: pcf8563: convert from round_rate() to determine_rate()
      rtc: rv3028: convert from round_rate() to determine_rate()
      rtc: rv3032: convert from round_rate() to determine_rate()

 drivers/rtc/rtc-ds1307.c   | 15 ++++++++++-----
 drivers/rtc/rtc-hym8563.c  | 15 ++++++++++-----
 drivers/rtc/rtc-m41t80.c   | 21 +++++++++++----------
 drivers/rtc/rtc-max31335.c | 12 +++++++-----
 drivers/rtc/rtc-nct3018y.c | 15 ++++++++++-----
 drivers/rtc/rtc-pcf85063.c | 15 ++++++++++-----
 drivers/rtc/rtc-pcf8563.c  | 15 ++++++++++-----
 drivers/rtc/rtc-rv3028.c   | 15 ++++++++++-----
 drivers/rtc/rtc-rv3032.c   | 21 +++++++++++++--------
 9 files changed, 91 insertions(+), 53 deletions(-)
---
base-commit: b551c4e2a98a177a06148cf16505643cd2108386
change-id: 20250710-rtc-clk-round-rate-1093066d1a5c

Best regards,
-- 
Brian Masney <bmasney@redhat.com>
Re: [PATCH 00/15] rtc: convert from clk round_rate() to determine_rate() and fix a few bugs
Posted by Alexandre Belloni 2 months ago
On Thu, 10 Jul 2025 11:20:20 -0400, Brian Masney wrote:
> The round_rate() clk ops is deprecated in the clk framework in favor
> of the determine_rate() clk ops, so let's go ahead and convert the
> drivers in the rtc subsystem using the Coccinelle semantic patch
> posted below. I did a few minor cosmetic cleanups of the code in a
> few cases.
> 
> I also noticed that in some of the drivers that if round_rate() is
> called with a requested rate higher than the highest supported rate,
> then the clock is disabled. According to the clk API, round_rate()
> should instead return the highest supported rate. This series also
> updates the functions to return the maximum supported rate.
> 
> [...]

Applied, thanks!

[01/15] rtc: ds1307: fix incorrect maximum clock rate handling
        https://git.kernel.org/abelloni/c/cf6eb547a24a
[02/15] rtc: hym8563: fix incorrect maximum clock rate handling
        https://git.kernel.org/abelloni/c/d0a518eb0a69
[03/15] rtc: nct3018y: fix incorrect maximum clock rate handling
        https://git.kernel.org/abelloni/c/437c59e4b222
[04/15] rtc: pcf85063: fix incorrect maximum clock rate handling
        https://git.kernel.org/abelloni/c/186ae1869880
[05/15] rtc: pcf8563: fix incorrect maximum clock rate handling
        https://git.kernel.org/abelloni/c/906726a5efee
[06/15] rtc: rv3028: fix incorrect maximum clock rate handling
        https://git.kernel.org/abelloni/c/b574acb3cf75
[07/15] rtc: ds1307: convert from round_rate() to determine_rate()
        https://git.kernel.org/abelloni/c/31b5fea399d5
[08/15] rtc: hym8563: convert from round_rate() to determine_rate()
        https://git.kernel.org/abelloni/c/394a4b920a72
[09/15] rtc: m41t80: convert from round_rate() to determine_rate()
        https://git.kernel.org/abelloni/c/e05d81b75efd
[10/15] rtc: max31335: convert from round_rate() to determine_rate()
        https://git.kernel.org/abelloni/c/9e0dfc7962b3
[11/15] rtc: nct3018y: convert from round_rate() to determine_rate()
        https://git.kernel.org/abelloni/c/1251d043f764
[12/15] rtc: pcf85063: convert from round_rate() to determine_rate()
        https://git.kernel.org/abelloni/c/ad853657d791
[13/15] rtc: pcf8563: convert from round_rate() to determine_rate()
        https://git.kernel.org/abelloni/c/e6f1af719ea1
[14/15] rtc: rv3028: convert from round_rate() to determine_rate()
        https://git.kernel.org/abelloni/c/c4253b091441
[15/15] rtc: rv3032: convert from round_rate() to determine_rate()
        https://git.kernel.org/abelloni/c/35d6aae85b36

Best regards,

-- 
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com