Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 813eec75 authored by Biao Long's avatar Biao Long
Browse files

devfreq: Support opp table for devfreq_simple_dev



Support read opp value for dev_target.
Use previous_freq instead of get_cur_freq when opp
table parsed success.

Change-Id: I37bb74ef18a822b05ed3be411bc59ae4dadb7a0d
Signed-off-by: default avatarBiao Long <blong@codeaurora.org>
parent 2c8ec678
Loading
Loading
Loading
Loading
+44 −9
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2014-2015, 2017-2018, 2019, The Linux Foundation. All rights reserved.
 * Copyright (c) 2014-2015, 2017-2018, 2019, 2021, The Linux Foundation. All rights reserved.
 */

#define pr_fmt(fmt) "devfreq-simple-dev: " fmt
@@ -22,11 +22,14 @@
#include <linux/clk.h>
#include <trace/events/power.h>

#define MBYTE (1UL << 20)

struct dev_data {
	struct clk			*clk;
	struct devfreq			*df;
	struct devfreq_dev_profile	profile;
	bool				freq_in_khz;
	unsigned int			width;
};

static void find_freq(struct devfreq_dev_profile *p, unsigned long *freq,
@@ -51,14 +54,35 @@ static void find_freq(struct devfreq_dev_profile *p, unsigned long *freq,
		*freq = atleast;
}

static u64 mbps_to_hz(u32 in, uint width)
{
	u64 result;
	u32 quot = in / width;
	u32 rem = in % width;

	result = quot * MBYTE + div_u64(rem * MBYTE, width);
	return result;
}

static int dev_target(struct device *dev, unsigned long *freq, u32 flags)
{
	struct dev_data *d = dev_get_drvdata(dev);
	struct dev_pm_opp *opp;
	unsigned long rfreq;
	u64 new_freq;

	opp = devfreq_recommended_opp(dev, freq, flags);
	if (!IS_ERR(opp))
		dev_pm_opp_put(opp);

	if (!d->freq_in_khz) {
		new_freq = mbps_to_hz(*freq, d->width);
	} else {
		find_freq(&d->profile, freq, flags);
		new_freq = *freq;
	}

	rfreq = clk_round_rate(d->clk, d->freq_in_khz ? *freq * 1000 : *freq);
	rfreq = clk_round_rate(d->clk, d->freq_in_khz ? new_freq * 1000 : new_freq);
	if (IS_ERR_VALUE(rfreq)) {
		dev_err(dev, "devfreq: Cannot find matching frequency for %lu\n",
			*freq);
@@ -89,11 +113,21 @@ static int parse_freq_table(struct device *dev, struct dev_data *d)
	unsigned long f;

	if (!of_find_property(dev->of_node, PROP_TBL, &len)) {
		ret = dev_pm_opp_of_add_table(dev);
		if (ret < 0)
			dev_err(dev, "Couldn't parse OPP table:%d\n", ret);
		if (dev_pm_opp_get_opp_count(dev) <= 0)
			return -EPROBE_DEFER;
		d->freq_in_khz = false;

		ret = of_property_read_u32(dev->of_node, "qcom,bus-width",
						&d->width);
		if (ret < 0 || !d->width) {
			dev_err(dev, "Missing or invalid bus-width: %d\n", ret);
			return -EINVAL;
		}
		return 0;
	}

	d->freq_in_khz = true;
	len /= sizeof(*data);
	data = devm_kzalloc(dev, len * sizeof(*data), GFP_KERNEL);
@@ -153,11 +187,12 @@ static int devfreq_clock_probe(struct platform_device *pdev)

	p = &d->profile;
	p->target = dev_target;
	if (d->freq_in_khz) {
		p->get_cur_freq = dev_get_cur_freq;
		ret = dev_get_cur_freq(dev, &p->initial_freq);
		if (ret < 0)
			return ret;

	}
	p->polling_ms = 50;
	if (!of_property_read_u32(dev->of_node, "polling-ms", &poll))
		p->polling_ms = poll;