Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Generic Exynos Bus frequency driver with DEVFREQ Framework
0004  *
0005  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
0006  * Author : Chanwoo Choi <cw00.choi@samsung.com>
0007  *
0008  * This driver support Exynos Bus frequency feature by using
0009  * DEVFREQ framework and is based on drivers/devfreq/exynos/exynos4_bus.c.
0010  */
0011 
0012 #include <linux/clk.h>
0013 #include <linux/devfreq.h>
0014 #include <linux/devfreq-event.h>
0015 #include <linux/device.h>
0016 #include <linux/export.h>
0017 #include <linux/module.h>
0018 #include <linux/of.h>
0019 #include <linux/pm_opp.h>
0020 #include <linux/platform_device.h>
0021 #include <linux/regulator/consumer.h>
0022 
0023 #define DEFAULT_SATURATION_RATIO    40
0024 
0025 struct exynos_bus {
0026     struct device *dev;
0027     struct platform_device *icc_pdev;
0028 
0029     struct devfreq *devfreq;
0030     struct devfreq_event_dev **edev;
0031     unsigned int edev_count;
0032     struct mutex lock;
0033 
0034     unsigned long curr_freq;
0035 
0036     int opp_token;
0037     struct clk *clk;
0038     unsigned int ratio;
0039 };
0040 
0041 /*
0042  * Control the devfreq-event device to get the current state of bus
0043  */
0044 #define exynos_bus_ops_edev(ops)                \
0045 static int exynos_bus_##ops(struct exynos_bus *bus)     \
0046 {                               \
0047     int i, ret;                     \
0048                                 \
0049     for (i = 0; i < bus->edev_count; i++) {         \
0050         if (!bus->edev[i])              \
0051             continue;               \
0052         ret = devfreq_event_##ops(bus->edev[i]);    \
0053         if (ret < 0)                    \
0054             return ret;             \
0055     }                           \
0056                                 \
0057     return 0;                       \
0058 }
0059 exynos_bus_ops_edev(enable_edev);
0060 exynos_bus_ops_edev(disable_edev);
0061 exynos_bus_ops_edev(set_event);
0062 
0063 static int exynos_bus_get_event(struct exynos_bus *bus,
0064                 struct devfreq_event_data *edata)
0065 {
0066     struct devfreq_event_data event_data;
0067     unsigned long load_count = 0, total_count = 0;
0068     int i, ret = 0;
0069 
0070     for (i = 0; i < bus->edev_count; i++) {
0071         if (!bus->edev[i])
0072             continue;
0073 
0074         ret = devfreq_event_get_event(bus->edev[i], &event_data);
0075         if (ret < 0)
0076             return ret;
0077 
0078         if (i == 0 || event_data.load_count > load_count) {
0079             load_count = event_data.load_count;
0080             total_count = event_data.total_count;
0081         }
0082     }
0083 
0084     edata->load_count = load_count;
0085     edata->total_count = total_count;
0086 
0087     return ret;
0088 }
0089 
0090 /*
0091  * devfreq function for both simple-ondemand and passive governor
0092  */
0093 static int exynos_bus_target(struct device *dev, unsigned long *freq, u32 flags)
0094 {
0095     struct exynos_bus *bus = dev_get_drvdata(dev);
0096     struct dev_pm_opp *new_opp;
0097     int ret = 0;
0098 
0099     /* Get correct frequency for bus. */
0100     new_opp = devfreq_recommended_opp(dev, freq, flags);
0101     if (IS_ERR(new_opp)) {
0102         dev_err(dev, "failed to get recommended opp instance\n");
0103         return PTR_ERR(new_opp);
0104     }
0105 
0106     dev_pm_opp_put(new_opp);
0107 
0108     /* Change voltage and frequency according to new OPP level */
0109     mutex_lock(&bus->lock);
0110     ret = dev_pm_opp_set_rate(dev, *freq);
0111     if (!ret)
0112         bus->curr_freq = *freq;
0113 
0114     mutex_unlock(&bus->lock);
0115 
0116     return ret;
0117 }
0118 
0119 static int exynos_bus_get_dev_status(struct device *dev,
0120                      struct devfreq_dev_status *stat)
0121 {
0122     struct exynos_bus *bus = dev_get_drvdata(dev);
0123     struct devfreq_event_data edata;
0124     int ret;
0125 
0126     stat->current_frequency = bus->curr_freq;
0127 
0128     ret = exynos_bus_get_event(bus, &edata);
0129     if (ret < 0) {
0130         dev_err(dev, "failed to get event from devfreq-event devices\n");
0131         stat->total_time = stat->busy_time = 0;
0132         goto err;
0133     }
0134 
0135     stat->busy_time = (edata.load_count * 100) / bus->ratio;
0136     stat->total_time = edata.total_count;
0137 
0138     dev_dbg(dev, "Usage of devfreq-event : %lu/%lu\n", stat->busy_time,
0139                             stat->total_time);
0140 
0141 err:
0142     ret = exynos_bus_set_event(bus);
0143     if (ret < 0) {
0144         dev_err(dev, "failed to set event to devfreq-event devices\n");
0145         return ret;
0146     }
0147 
0148     return ret;
0149 }
0150 
0151 static void exynos_bus_exit(struct device *dev)
0152 {
0153     struct exynos_bus *bus = dev_get_drvdata(dev);
0154     int ret;
0155 
0156     ret = exynos_bus_disable_edev(bus);
0157     if (ret < 0)
0158         dev_warn(dev, "failed to disable the devfreq-event devices\n");
0159 
0160     platform_device_unregister(bus->icc_pdev);
0161 
0162     dev_pm_opp_of_remove_table(dev);
0163     clk_disable_unprepare(bus->clk);
0164     dev_pm_opp_put_regulators(bus->opp_token);
0165 }
0166 
0167 static void exynos_bus_passive_exit(struct device *dev)
0168 {
0169     struct exynos_bus *bus = dev_get_drvdata(dev);
0170 
0171     platform_device_unregister(bus->icc_pdev);
0172 
0173     dev_pm_opp_of_remove_table(dev);
0174     clk_disable_unprepare(bus->clk);
0175 }
0176 
0177 static int exynos_bus_parent_parse_of(struct device_node *np,
0178                     struct exynos_bus *bus)
0179 {
0180     struct device *dev = bus->dev;
0181     const char *supplies[] = { "vdd", NULL };
0182     int i, ret, count, size;
0183 
0184     ret = dev_pm_opp_set_regulators(dev, supplies);
0185     if (ret < 0) {
0186         dev_err(dev, "failed to set regulators %d\n", ret);
0187         return ret;
0188     }
0189 
0190     bus->opp_token = ret;
0191 
0192     /*
0193      * Get the devfreq-event devices to get the current utilization of
0194      * buses. This raw data will be used in devfreq ondemand governor.
0195      */
0196     count = devfreq_event_get_edev_count(dev, "devfreq-events");
0197     if (count < 0) {
0198         dev_err(dev, "failed to get the count of devfreq-event dev\n");
0199         ret = count;
0200         goto err_regulator;
0201     }
0202     bus->edev_count = count;
0203 
0204     size = sizeof(*bus->edev) * count;
0205     bus->edev = devm_kzalloc(dev, size, GFP_KERNEL);
0206     if (!bus->edev) {
0207         ret = -ENOMEM;
0208         goto err_regulator;
0209     }
0210 
0211     for (i = 0; i < count; i++) {
0212         bus->edev[i] = devfreq_event_get_edev_by_phandle(dev,
0213                             "devfreq-events", i);
0214         if (IS_ERR(bus->edev[i])) {
0215             ret = -EPROBE_DEFER;
0216             goto err_regulator;
0217         }
0218     }
0219 
0220     /*
0221      * Optionally, Get the saturation ratio according to Exynos SoC
0222      * When measuring the utilization of each AXI bus with devfreq-event
0223      * devices, the measured real cycle might be much lower than the
0224      * total cycle of bus during sampling rate. In result, the devfreq
0225      * simple-ondemand governor might not decide to change the current
0226      * frequency due to too utilization (= real cycle/total cycle).
0227      * So, this property is used to adjust the utilization when calculating
0228      * the busy_time in exynos_bus_get_dev_status().
0229      */
0230     if (of_property_read_u32(np, "exynos,saturation-ratio", &bus->ratio))
0231         bus->ratio = DEFAULT_SATURATION_RATIO;
0232 
0233     return 0;
0234 
0235 err_regulator:
0236     dev_pm_opp_put_regulators(bus->opp_token);
0237 
0238     return ret;
0239 }
0240 
0241 static int exynos_bus_parse_of(struct device_node *np,
0242                   struct exynos_bus *bus)
0243 {
0244     struct device *dev = bus->dev;
0245     struct dev_pm_opp *opp;
0246     unsigned long rate;
0247     int ret;
0248 
0249     /* Get the clock to provide each bus with source clock */
0250     bus->clk = devm_clk_get(dev, "bus");
0251     if (IS_ERR(bus->clk)) {
0252         dev_err(dev, "failed to get bus clock\n");
0253         return PTR_ERR(bus->clk);
0254     }
0255 
0256     ret = clk_prepare_enable(bus->clk);
0257     if (ret < 0) {
0258         dev_err(dev, "failed to get enable clock\n");
0259         return ret;
0260     }
0261 
0262     /* Get the freq and voltage from OPP table to scale the bus freq */
0263     ret = dev_pm_opp_of_add_table(dev);
0264     if (ret < 0) {
0265         dev_err(dev, "failed to get OPP table\n");
0266         goto err_clk;
0267     }
0268 
0269     rate = clk_get_rate(bus->clk);
0270 
0271     opp = devfreq_recommended_opp(dev, &rate, 0);
0272     if (IS_ERR(opp)) {
0273         dev_err(dev, "failed to find dev_pm_opp\n");
0274         ret = PTR_ERR(opp);
0275         goto err_opp;
0276     }
0277     bus->curr_freq = dev_pm_opp_get_freq(opp);
0278     dev_pm_opp_put(opp);
0279 
0280     return 0;
0281 
0282 err_opp:
0283     dev_pm_opp_of_remove_table(dev);
0284 err_clk:
0285     clk_disable_unprepare(bus->clk);
0286 
0287     return ret;
0288 }
0289 
0290 static int exynos_bus_profile_init(struct exynos_bus *bus,
0291                    struct devfreq_dev_profile *profile)
0292 {
0293     struct device *dev = bus->dev;
0294     struct devfreq_simple_ondemand_data *ondemand_data;
0295     int ret;
0296 
0297     /* Initialize the struct profile and governor data for parent device */
0298     profile->polling_ms = 50;
0299     profile->target = exynos_bus_target;
0300     profile->get_dev_status = exynos_bus_get_dev_status;
0301     profile->exit = exynos_bus_exit;
0302 
0303     ondemand_data = devm_kzalloc(dev, sizeof(*ondemand_data), GFP_KERNEL);
0304     if (!ondemand_data)
0305         return -ENOMEM;
0306 
0307     ondemand_data->upthreshold = 40;
0308     ondemand_data->downdifferential = 5;
0309 
0310     /* Add devfreq device to monitor and handle the exynos bus */
0311     bus->devfreq = devm_devfreq_add_device(dev, profile,
0312                         DEVFREQ_GOV_SIMPLE_ONDEMAND,
0313                         ondemand_data);
0314     if (IS_ERR(bus->devfreq)) {
0315         dev_err(dev, "failed to add devfreq device\n");
0316         return PTR_ERR(bus->devfreq);
0317     }
0318 
0319     /* Register opp_notifier to catch the change of OPP  */
0320     ret = devm_devfreq_register_opp_notifier(dev, bus->devfreq);
0321     if (ret < 0) {
0322         dev_err(dev, "failed to register opp notifier\n");
0323         return ret;
0324     }
0325 
0326     /*
0327      * Enable devfreq-event to get raw data which is used to determine
0328      * current bus load.
0329      */
0330     ret = exynos_bus_enable_edev(bus);
0331     if (ret < 0) {
0332         dev_err(dev, "failed to enable devfreq-event devices\n");
0333         return ret;
0334     }
0335 
0336     ret = exynos_bus_set_event(bus);
0337     if (ret < 0) {
0338         dev_err(dev, "failed to set event to devfreq-event devices\n");
0339         goto err_edev;
0340     }
0341 
0342     return 0;
0343 
0344 err_edev:
0345     if (exynos_bus_disable_edev(bus))
0346         dev_warn(dev, "failed to disable the devfreq-event devices\n");
0347 
0348     return ret;
0349 }
0350 
0351 static int exynos_bus_profile_init_passive(struct exynos_bus *bus,
0352                        struct devfreq_dev_profile *profile)
0353 {
0354     struct device *dev = bus->dev;
0355     struct devfreq_passive_data *passive_data;
0356     struct devfreq *parent_devfreq;
0357 
0358     /* Initialize the struct profile and governor data for passive device */
0359     profile->target = exynos_bus_target;
0360     profile->exit = exynos_bus_passive_exit;
0361 
0362     /* Get the instance of parent devfreq device */
0363     parent_devfreq = devfreq_get_devfreq_by_phandle(dev, "devfreq", 0);
0364     if (IS_ERR(parent_devfreq))
0365         return -EPROBE_DEFER;
0366 
0367     passive_data = devm_kzalloc(dev, sizeof(*passive_data), GFP_KERNEL);
0368     if (!passive_data)
0369         return -ENOMEM;
0370 
0371     passive_data->parent = parent_devfreq;
0372 
0373     /* Add devfreq device for exynos bus with passive governor */
0374     bus->devfreq = devm_devfreq_add_device(dev, profile, DEVFREQ_GOV_PASSIVE,
0375                         passive_data);
0376     if (IS_ERR(bus->devfreq)) {
0377         dev_err(dev,
0378             "failed to add devfreq dev with passive governor\n");
0379         return PTR_ERR(bus->devfreq);
0380     }
0381 
0382     return 0;
0383 }
0384 
0385 static int exynos_bus_probe(struct platform_device *pdev)
0386 {
0387     struct device *dev = &pdev->dev;
0388     struct device_node *np = dev->of_node, *node;
0389     struct devfreq_dev_profile *profile;
0390     struct exynos_bus *bus;
0391     int ret, max_state;
0392     unsigned long min_freq, max_freq;
0393     bool passive = false;
0394 
0395     if (!np) {
0396         dev_err(dev, "failed to find devicetree node\n");
0397         return -EINVAL;
0398     }
0399 
0400     bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL);
0401     if (!bus)
0402         return -ENOMEM;
0403     mutex_init(&bus->lock);
0404     bus->dev = &pdev->dev;
0405     platform_set_drvdata(pdev, bus);
0406 
0407     profile = devm_kzalloc(dev, sizeof(*profile), GFP_KERNEL);
0408     if (!profile)
0409         return -ENOMEM;
0410 
0411     node = of_parse_phandle(dev->of_node, "devfreq", 0);
0412     if (node) {
0413         of_node_put(node);
0414         passive = true;
0415     } else {
0416         ret = exynos_bus_parent_parse_of(np, bus);
0417         if (ret < 0)
0418             return ret;
0419     }
0420 
0421     /* Parse the device-tree to get the resource information */
0422     ret = exynos_bus_parse_of(np, bus);
0423     if (ret < 0)
0424         goto err_reg;
0425 
0426     if (passive)
0427         ret = exynos_bus_profile_init_passive(bus, profile);
0428     else
0429         ret = exynos_bus_profile_init(bus, profile);
0430 
0431     if (ret < 0)
0432         goto err;
0433 
0434     /* Create child platform device for the interconnect provider */
0435     if (of_get_property(dev->of_node, "#interconnect-cells", NULL)) {
0436         bus->icc_pdev = platform_device_register_data(
0437                         dev, "exynos-generic-icc",
0438                         PLATFORM_DEVID_AUTO, NULL, 0);
0439 
0440         if (IS_ERR(bus->icc_pdev)) {
0441             ret = PTR_ERR(bus->icc_pdev);
0442             goto err;
0443         }
0444     }
0445 
0446     max_state = bus->devfreq->max_state;
0447     min_freq = (bus->devfreq->freq_table[0] / 1000);
0448     max_freq = (bus->devfreq->freq_table[max_state - 1] / 1000);
0449     pr_info("exynos-bus: new bus device registered: %s (%6ld KHz ~ %6ld KHz)\n",
0450             dev_name(dev), min_freq, max_freq);
0451 
0452     return 0;
0453 
0454 err:
0455     dev_pm_opp_of_remove_table(dev);
0456     clk_disable_unprepare(bus->clk);
0457 err_reg:
0458     dev_pm_opp_put_regulators(bus->opp_token);
0459 
0460     return ret;
0461 }
0462 
0463 static void exynos_bus_shutdown(struct platform_device *pdev)
0464 {
0465     struct exynos_bus *bus = dev_get_drvdata(&pdev->dev);
0466 
0467     devfreq_suspend_device(bus->devfreq);
0468 }
0469 
0470 #ifdef CONFIG_PM_SLEEP
0471 static int exynos_bus_resume(struct device *dev)
0472 {
0473     struct exynos_bus *bus = dev_get_drvdata(dev);
0474     int ret;
0475 
0476     ret = exynos_bus_enable_edev(bus);
0477     if (ret < 0) {
0478         dev_err(dev, "failed to enable the devfreq-event devices\n");
0479         return ret;
0480     }
0481 
0482     return 0;
0483 }
0484 
0485 static int exynos_bus_suspend(struct device *dev)
0486 {
0487     struct exynos_bus *bus = dev_get_drvdata(dev);
0488     int ret;
0489 
0490     ret = exynos_bus_disable_edev(bus);
0491     if (ret < 0) {
0492         dev_err(dev, "failed to disable the devfreq-event devices\n");
0493         return ret;
0494     }
0495 
0496     return 0;
0497 }
0498 #endif
0499 
0500 static const struct dev_pm_ops exynos_bus_pm = {
0501     SET_SYSTEM_SLEEP_PM_OPS(exynos_bus_suspend, exynos_bus_resume)
0502 };
0503 
0504 static const struct of_device_id exynos_bus_of_match[] = {
0505     { .compatible = "samsung,exynos-bus", },
0506     { /* sentinel */ },
0507 };
0508 MODULE_DEVICE_TABLE(of, exynos_bus_of_match);
0509 
0510 static struct platform_driver exynos_bus_platdrv = {
0511     .probe      = exynos_bus_probe,
0512     .shutdown   = exynos_bus_shutdown,
0513     .driver = {
0514         .name   = "exynos-bus",
0515         .pm = &exynos_bus_pm,
0516         .of_match_table = of_match_ptr(exynos_bus_of_match),
0517     },
0518 };
0519 module_platform_driver(exynos_bus_platdrv);
0520 
0521 MODULE_DESCRIPTION("Generic Exynos Bus frequency driver");
0522 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
0523 MODULE_LICENSE("GPL v2");