Re: [PATCH 3/7] soc: mediatek: add MMDVFS core driver

From: Echo Zhang (张玉)

Date: Thu Nov 26 2026 - 04:14:48 EST


On 三, 2026-11-25 at 08:35 +0000, Roman Vivchar wrote:
>
> External email : Please do not click links or open attachments until
> you have verified the sender or the content.
>
>
> On Wednesday, November 25th, 2026 at 8:01 AM, Echo Zhang
> <echo.zhang@xxxxxxxxxxxx> wrote:
>
> > Add the common MediaTek MMDVFS driver framework.
> >
> > The core driver handles device probing, SoC data selection, clock
> > provider
> > registration, power-management notification, and dispatch to the
> > microprocessor transport used for runtime performance voting.
> >
> > Signed-off-by: Echo Zhang <echo.zhang@xxxxxxxxxxxx>
> > ---
>
> ...
>
> > diff --git a/drivers/soc/mediatek/mmdvfs/mmdvfs-core.c
> > b/drivers/soc/mediatek/mmdvfs/mmdvfs-core.c
> > new file mode 100644
> > index 000000000000..c32fa11557af
> > --- /dev/null
> > +++ b/drivers/soc/mediatek/mmdvfs/mmdvfs-core.c
> > @@ -0,0 +1,581 @@
> > +// SPDX-License-Identifier: GPL-2.0-only
> > +/*
> > + * Copyright (c) 2026 MediaTek Inc.
> > + *
> > + * MediaTek MMDVFS Core Framework
> > + */
> > +
> > +#include <linux/atomic.h>
> > +#include <linux/clk-provider.h>
> > +#include <linux/device.h>
> > +#include <linux/err.h>
> > +#include <linux/io.h>
> > +#include <linux/module.h>
> > +#include <linux/mutex.h>
> > +#include <linux/notifier.h>
> > +#include <linux/of.h>
> > +#include <linux/of_address.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/sched/clock.h>
> > +#include <linux/slab.h>
> > +#include <linux/string.h>
> > +#include <linux/suspend.h>
> > +#include <linux/workqueue.h>
> > +
> > +#include "mmdvfs-internal.h"
> > +
> > +static int mmdvfs_register_device(struct mmdvfs_device *mmdvfs);
> > +static void mmdvfs_unregister_device(struct mmdvfs_device
> > *mmdvfs);
> > +
> > +struct mmdvfs_device *mmdvfs_find_device_by_version(enum
> > mmdvfs_version_type version);
> > +struct mmdvfs_device *mmdvfs_find_device_any(void);
> > +
> > +struct mmdvfs_device *mmdvfs_find_device_any(void)
> > +{
> > +     return
> > mmdvfs_find_device_by_version(MMDVFS_VERSION_HWCCF_DVFS_VOTER);
> > +}
> > +EXPORT_SYMBOL_GPL(mmdvfs_find_device_any);
>
> Ambiguous function name. any would mean *any* version, but it uses
> hardware here.
>
> > +
> > +/**
> > + * mmdvfs_get_version - Get MMDVFS voting mechanism type
> > + *
> > + * Return:
> > + *   0 - No hardware voter
> > + *   1 - HWCCF DVFS voter
> > + */
> > +u32 mmdvfs_get_version(void)
> > +{
> > +     struct mmdvfs_device *mmdvfs;
> > +
> > +     mmdvfs = mmdvfs_find_device_any();
> > +     if (!mmdvfs || !mmdvfs->data)
> > +             return MMDVFS_VERSION_NONE;
> > +
> > +     return mmdvfs->data->version;
> > +}
> > +EXPORT_SYMBOL_GPL(mmdvfs_get_version);
> > +
> > +/*
> > + * mmdvfs_validate_topology - Validate RC/MUX/User topology
> > consistency
> > + * @mmdvfs: MMDVFS device structure
> > + *
> > + * Return: 0 on success, -EINVAL if topology validation fails
> > + */
> > +static int mmdvfs_validate_topology(struct mmdvfs_device *mmdvfs)
> > +{
> > +     struct mmdvfs_data *data = mmdvfs->data;
> > +     int i, j;
> > +
> > +     if (!data || !data->mux || !data->rc) {
> > +             mmdvfs_err(mmdvfs->dev, "Invalid data structures for
> > validation\n");
> > +             return -EINVAL;
> > +     }
> > +
> > +     for (i = 0; i < data->mux_num; i++) {
> > +             struct mmdvfs_mux *mux = &data->mux[i];
> > +             struct mmdvfs_rc *rc;
> > +
> > +             if (!mux->name || strlen(mux->name) == 0) {
> > +                     mmdvfs_err(mmdvfs->dev, "MUX %d has empty
> > name\n", i);
> > +                     return -EINVAL;
> > +             }
> > +
> > +             if (mux->rc >= data->rc_num) {
> > +                     mmdvfs_err(mmdvfs->dev,
> > +                                "MUX %d (%s) has invalid RC
> > reference: %d (max: %d)\n",
> > +                                i, mux->name, mux->rc, data-
> > >rc_num);
> > +                     return -EINVAL;
> > +             }
> > +
> > +             rc = &data->rc[mux->rc];
> > +
> > +             if (rc->level_num == 0 || rc->level_num >
> > MMDVFS_MAX_LEVELS) {
> > +                     mmdvfs_err(mmdvfs->dev,
> > +                                "MUX %d (%s): RC %d has invalid
> > level_num: %d\n",
> > +                                i, mux->name, mux->rc, rc-
> > >level_num);
> > +                     return -EINVAL;
> > +             }
> > +
> > +             if (mux->freq_num == 0 || mux->freq_num >
> > MMDVFS_MAX_LEVELS) {
> > +                     mmdvfs_err(mmdvfs->dev,
> > +                                "MUX %d (%s): invalid freq_num:
> > %d\n",
> > +                                i, mux->name, mux->freq_num);
> > +                     return -EINVAL;
> > +             }
> > +
> > +             for (j = 0; j < mux->freq_num; j++) {
> > +                     if (mux->freq[j] == 0) {
> > +                             mmdvfs_err(mmdvfs->dev,
> > +                                        "MUX %d (%s): incomplete
> > freq table at level %d/%d (freq=0)\n",
> > +                                        i, mux->name, j, mux-
> > >freq_num);
> > +                             return -EINVAL;
> > +                     }
> > +
> > +                     if (j > 0 && mux->freq[j] <= mux->freq[j -
> > 1]) {
> > +                             mmdvfs_warn(mmdvfs->dev,
> > +                                         "MUX %d (%s): freq not
> > ascending: freq[%d]=%llu <= freq[%d]=%llu\n",
> > +                                         i, mux->name, j, mux-
> > >freq[j],
> > +                                         j - 1, mux->freq[j - 1]);
> > +                     }
> > +             }
> > +
> > +             mmdvfs_dbg(mmdvfs->dev,
> > +                        "MUX[%d] validated: name=%s, rc=%d,
> > freq_num=%d (RC levels=%d), freq_min=%llu, freq_max=%llu\n",
> > +                        i, mux->name, mux->rc, mux->freq_num,
> > +                        rc->level_num, mux->freq[0],
> > +                        mux->freq[mux->freq_num - 1]);
> > +     }
> > +
> > +     for (i = 0; i < data->user_num; i++) {
> > +             struct mmdvfs_user *user = &data->user[i];
> > +
> > +             if (!user->name || strlen(user->name) == 0) {
> > +                     mmdvfs_err(mmdvfs->dev, "User %d has empty
> > name\n", i);
> > +                     return -EINVAL;
> > +             }
> > +
> > +             if (user->mux >= data->mux_num) {
> > +                     mmdvfs_err(mmdvfs->dev,
> > +                                "User %d (%s) has invalid MUX
> > reference: %d (max: %d)\n",
> > +                                i, user->name, user->mux, data-
> > >mux_num);
> > +                     return -EINVAL;
> > +             }
> > +
> > +             mmdvfs_dbg(mmdvfs->dev,
> > +                        "User[%d] validated: name=%s, id=%d,
> > mux=%d\n",
> > +                        i, user->name, user->id, user->mux);
> > +     }
> > +
> > +     mmdvfs_info(mmdvfs->dev, "Topology validation successful\n");
>
> Do we really need these logs? This applies to the rest of the driver
> too.
>
> ...
>
> > +/*
> > + * mmdvfs_parse_dt - Parse device tree configuration
> > + * @mmdvfs: MMDVFS device structure
> > + *
> > + * Return: 0 on success, negative error code on failure
> > + */
> > +int mmdvfs_parse_dt(struct mmdvfs_device *mmdvfs)
> > +{
> > +     struct device_node *node = mmdvfs->dev->of_node;
> > +
> > +     if (!node) {
> > +             mmdvfs_err(mmdvfs->dev, "No device tree node
> > found\n");
> > +             return -ENODEV;
> > +     }
> > +
> > +     mmdvfs_info(mmdvfs->dev, "Device tree parsed (hardware
> > attributes)\n");
> > +
> > +     return 0;
>
> This is not DT parsing.
>
> > +}
> > +
> > +/*
> > + * mmdvfs_pm_notifier_callback - PM notifier callback for
> > suspend/resume handling
> > + * @nb: Notifier block
> > + * @event: PM event type
> > + * @data: Event data
> > + *
> > + * Return: NOTIFY_DONE
> > + */
> > +static int mmdvfs_pm_notifier_callback(struct notifier_block *nb,
> > unsigned long event,
> > +                                    void *data)
> > +{
> > +     struct mmdvfs_data *mdata =
> > +             container_of(nb, struct mmdvfs_data, pm_notifier);
> > +     struct mmdvfs_device *mmdvfs = dev_get_drvdata(mdata->dev);
> > +
> > +     switch (event) {
> > +     case PM_SUSPEND_PREPARE:
> > +             mmdvfs_dbg(mmdvfs->dev, "System suspending...\n");
> > +
> > +             if (mdata->ops && mdata->ops->suspend_prepare)
> > +                     mdata->ops->suspend_prepare(mmdvfs);
> > +
> > +             mdata->pm_suspended = true;
> > +             break;
> > +
> > +     case PM_POST_SUSPEND:
> > +             mmdvfs_dbg(mmdvfs->dev, "System resumed\n");
> > +
> > +             mdata->pm_suspended = false;
> > +
> > +             if (mdata->ops && mdata->ops->post_suspend)
> > +                     mdata->ops->post_suspend(mmdvfs);
> > +             break;
> > +
> > +     default:
> > +             break;
> > +     }
> > +
> > +     return NOTIFY_DONE;
> > +}
> > +
> > +/*
> > + * mmdvfs_probe - Main probe function
> > + * @pdev: Platform device
> > + *
> > + * Return: 0 on success, negative error code on failure
> > + */
> > +static int mmdvfs_probe(struct platform_device *pdev)
> > +{
> > +     struct mmdvfs_device *mmdvfs;
> > +     struct mmdvfs_data *data;
> > +     const struct mmdvfs_platform_data *pdata;
> > +     int ret;
> > +
> > +     mmdvfs = devm_kzalloc(&pdev->dev, sizeof(*mmdvfs),
> > GFP_KERNEL);
> > +     if (!mmdvfs)
> > +             return -ENOMEM;
> > +
> > +     data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
> > +     if (!data)
> > +             return -ENOMEM;
> > +
> > +     mmdvfs->dev = &pdev->dev;
> > +     mmdvfs->data = data;
> > +     data->dev = &pdev->dev;
> > +
> > +     mutex_init(&data->lock);
> > +
> > +     platform_set_drvdata(pdev, mmdvfs);
> > +
> > +     pdata = of_device_get_match_data(&pdev->dev);
> > +     if (!pdata) {
>
> This is not possible, since you already have mt8196 platform data in
> the table. Drop the check.
>
> > +             mmdvfs_err(mmdvfs->dev, "No matching platform
> > data\n");
> > +             return -ENODEV;
> > +     }
> > +
> > +     data->ops = pdata->ops;
> > +     data->version = pdata->soc_data->version;
> > +
> > +     if (data->version == MMDVFS_VERSION_HWCCF_DVFS_VOTER) {
> > +             data->hwccf_voter_priv = devm_kzalloc(&pdev->dev,
> > +                                                   sizeof(*data-
> > >hwccf_voter_priv),
> > +                                                   GFP_KERNEL);
> > +             if (!data->hwccf_voter_priv)
> > +                     return -ENOMEM;
> > +
> > +             data->hwccf_voter_priv->mmdvfs = mmdvfs;
> > +     }
> > +
> > +     ret = mmdvfs_parse_dt(mmdvfs);
> > +     if (ret) {
> > +             mmdvfs_err(mmdvfs->dev, "Failed to parse device tree:
> > %d\n", ret);
> > +             return ret;
>
> dev_err_probe
>
> > +     }
> > +
> > +     ret = mmdvfs_init_from_soc_data(mmdvfs, pdata->soc_data);
> > +     if (ret) {
> > +             mmdvfs_err(mmdvfs->dev, "Failed to initialize from
> > SoC data: %d\n", ret);
> > +             return ret;
>
> ...same...
>
> > +     }
> > +
> > +     if (data->ops && data->ops->init_hw) {
> > +             ret = data->ops->init_hw(mmdvfs);
> > +             if (ret) {
> > +                     mmdvfs_err(mmdvfs->dev, "Failed to initialize
> > hardware: %d\n", ret);
> > +                     return ret;
>
> here too.
>
> > +             }
> > +     }
> > +
> > +     data->init_done = true;
> > +
> > +     ret = mmdvfs_register_clocks(mmdvfs);
> > +     if (ret) {
> > +             mmdvfs_err(mmdvfs->dev, "Failed to register clocks:
> > %d\n", ret);
> > +             goto err_deinit_hw;
> > +     }
> > +
> > +     data->pm_notifier.notifier_call =
> > mmdvfs_pm_notifier_callback;
> > +     data->pm_notifier.priority = 0;
> > +     data->pm_suspended = false;
> > +     ret = register_pm_notifier(&data->pm_notifier);
> > +     if (ret) {
> > +             mmdvfs_err(mmdvfs->dev, "Failed to register PM
> > notifier: %d\n", ret);
> > +             goto err_unregister_clocks;
> > +     }
> > +     mmdvfs_info(mmdvfs->dev, "PM notifier registered\n");
> > +
> > +     ret = mmdvfs_register_device(mmdvfs);
> > +     if (ret) {
> > +             mmdvfs_err(mmdvfs->dev, "Failed to register device:
> > %d\n", ret);
> > +             goto err_unregister_pm_notifier;
> > +     }
> > +
> > +     mmdvfs_info(mmdvfs->dev, "MMDVFS driver probed
> > successfully\n");
> > +     return 0;
> > +
> > +err_unregister_pm_notifier:
> > +     unregister_pm_notifier(&data->pm_notifier);
> > +
> > +err_unregister_clocks:
> > +     mmdvfs_unregister_clocks(mmdvfs);
> > +
> > +err_deinit_hw:
> > +     if (data->ops && data->ops->deinit_hw)
> > +             data->ops->deinit_hw(mmdvfs);
> > +
> > +     return ret;
> > +}
> > +
>
> ...
>
> > +struct mmdvfs_device *mmdvfs_find_device_by_version(enum
> > mmdvfs_version_type version)
> > +{
> > +     struct mmdvfs_device_node *node;
> > +     struct mmdvfs_device *found = NULL;
> > +
> > +     mutex_lock(&mmdvfs_device_list_lock);
>
> guard(mutex)(&mmdvfs_device_list_lock)
>
> > +     list_for_each_entry(node, &mmdvfs_device_list, list) {
> > +             if (node->mmdvfs && node->mmdvfs->data &&
> > +                 node->mmdvfs->data->version == version) {
> > +                     found = node->mmdvfs;
> > +                     break;
> > +             }
> > +     }
> > +     mutex_unlock(&mmdvfs_device_list_lock);
> > +
> > +     return found;
> > +}
> > +EXPORT_SYMBOL_GPL(mmdvfs_find_device_by_version);
> > +
>
> ...
>
> > diff --git a/drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h
> > b/drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h
> > new file mode 100644
> > index 000000000000..df16e38d78c8
> > --- /dev/null
> > +++ b/drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h
> > @@ -0,0 +1,305 @@
> > +/* SPDX-License-Identifier: GPL-2.0-only */
> > +/*
> > + * Copyright (c) 2026 MediaTek Inc.
> > + *
> > + * MediaTek MMDVFS (MultiMedia Dynamic Voltage and Frequency
> > Scaling) Driver
> > + */
> > +
> > +#ifndef __MMDVFS_H__
> > +#define __MMDVFS_H__
> > +
> > +#include <linux/clk-provider.h>
> > +#include <linux/device.h>
> > +#include <linux/mutex.h>
> > +#include <linux/notifier.h>
> > +#include <linux/platform_device.h>
> > +
> > +struct dentry;
> > +
> > +#define MMDVFS_MAX_LEVELS            8
> > +#define MMDVFS_MAX_USERS             64
> > +#define MMDVFS_MAX_MUXES             16
> > +#define MMDVFS_MAX_RCS                       4
> > +
> > +/* MMDVFS voting mechanism types */
> > +enum mmdvfs_version_type {
> > +     MMDVFS_VERSION_NONE = 0,                /* No hardware voter
> > */
> > +     MMDVFS_VERSION_HWCCF_DVFS_VOTER = 1,    /* HWCCF DVFS voter
> > */
> > +};
> > +
> > +#define MAX_OPP                              8
> > +
> > +/* MMDVFS RC domains */
> > +enum mmdvfs_rc_domain {
> > +     MMDVFS_PWR_VCORE = 0,  /* PWR_MMDVFS_VCORE */
> > +     MMDVFS_PWR_VMM   = 1,  /* PWR_MMDVFS_VMM */
> > +     MMDVFS_PWR_VDISP = 2,  /* PWR_MMDVFS_VDISP */
> > +     MMDVFS_PWR_NUM   = 3   /* PWR_MMDVFS_NUM */
> > +};
> > +
> > +/**
> > + * struct mmdvfs_rc - Resource Controller
> > + * @id: RC identifier
> > + * @pa: Physical address base
> > + * @level_num: Number of supported levels
> > + * @rc_base: Virtual address base (mapped from pa)
> > + */
> > +struct mmdvfs_rc {
> > +     u8 id;
> > +     u32 pa;
>
> phys_addr_t
>
> > +     u8 level_num;
> > +     void __iomem *rc_base;
> > +};
>
> ...
>
> > +/* Logging macros */
> > +#define mmdvfs_dbg(dev, fmt, args...) \
> > +     dev_dbg(dev, "[mmdvfs] " fmt, ##args)
> > +#define mmdvfs_info(dev, fmt, args...) \
> > +     dev_info(dev, "[mmdvfs] " fmt, ##args)
> > +#define mmdvfs_warn(dev, fmt, args...) \
> > +     dev_warn(dev, "[mmdvfs] " fmt, ##args)
> > +#define mmdvfs_err(dev, fmt, args...) \
> > +     dev_err(dev, "[mmdvfs] " fmt, ##args)
>
> The driver name is already printed when using dev_* APIs. Use them
> directly.
>
> Best regards,
> Roman

Hi Roman,

Thank you for the detailed comments. I will address these in v2:

- Rename or remove mmdvfs_find_device_any() so its behavior is clear.
- Remove the routine success logs and use dev_*() directly instead of
the mmdvfs logging wrappers.
- Remove mmdvfs_parse_dt(), which currently does not parse DT data.
- Simplify the match-data handling, remove the redundant check, and use
dev_err_probe() for probe failures.
- Use guard(mutex) for the short device-list operations, and change the
stored physical address to phys_addr_t.

I will review the same patterns throughout the driver rather than
changing only the lines you pointed out.

Thanks,
Echo