Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Linux/drivers/clk/   (Open Source Betriebssystem Version 6.17.9©)  Datei vom 24.10.2025 mit Größe 4 kB image not shown  

Quelle  clk-pwm.c   Sprache: C

 
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (C) 2014 Philipp Zabel, Pengutronix
 *
 * PWM (mis)used as clock output
 */

#include <linux/clk-provider.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/pwm.h>

struct clk_pwm {
 struct clk_hw hw;
 struct pwm_device *pwm;
 struct pwm_state state;
 u32 fixed_rate;
};

static inline struct clk_pwm *to_clk_pwm(struct clk_hw *hw)
{
 return container_of(hw, struct clk_pwm, hw);
}

static int clk_pwm_enable(struct clk_hw *hw)
{
 struct clk_pwm *clk_pwm = to_clk_pwm(hw);

 return pwm_apply_atomic(clk_pwm->pwm, &clk_pwm->state);
}

static void clk_pwm_disable(struct clk_hw *hw)
{
 struct clk_pwm *clk_pwm = to_clk_pwm(hw);
 struct pwm_state state = clk_pwm->state;

 state.enabled = false;

 pwm_apply_atomic(clk_pwm->pwm, &state);
}

static int clk_pwm_prepare(struct clk_hw *hw)
{
 struct clk_pwm *clk_pwm = to_clk_pwm(hw);

 return pwm_apply_might_sleep(clk_pwm->pwm, &clk_pwm->state);
}

static void clk_pwm_unprepare(struct clk_hw *hw)
{
 struct clk_pwm *clk_pwm = to_clk_pwm(hw);

 pwm_disable(clk_pwm->pwm);
}

static unsigned long clk_pwm_recalc_rate(struct clk_hw *hw,
      unsigned long parent_rate)
{
 struct clk_pwm *clk_pwm = to_clk_pwm(hw);

 return clk_pwm->fixed_rate;
}

static int clk_pwm_get_duty_cycle(struct clk_hw *hw, struct clk_duty *duty)
{
 struct clk_pwm *clk_pwm = to_clk_pwm(hw);
 struct pwm_state state;
 int ret;

 ret = pwm_get_state_hw(clk_pwm->pwm, &state);
 if (ret)
  return ret;

 duty->num = state.duty_cycle;
 duty->den = state.period;

 return 0;
}

static const struct clk_ops clk_pwm_ops_atomic = {
 .enable = clk_pwm_enable,
 .disable = clk_pwm_disable,
 .recalc_rate = clk_pwm_recalc_rate,
 .get_duty_cycle = clk_pwm_get_duty_cycle,
};

static const struct clk_ops clk_pwm_ops = {
 .prepare = clk_pwm_prepare,
 .unprepare = clk_pwm_unprepare,
 .recalc_rate = clk_pwm_recalc_rate,
 .get_duty_cycle = clk_pwm_get_duty_cycle,
};

static int clk_pwm_probe(struct platform_device *pdev)
{
 struct device_node *node = pdev->dev.of_node;
 struct clk_init_data init;
 struct clk_pwm *clk_pwm;
 struct pwm_device *pwm;
 struct pwm_args pargs;
 const char *clk_name;
 int ret;

 clk_pwm = devm_kzalloc(&pdev->dev, sizeof(*clk_pwm), GFP_KERNEL);
 if (!clk_pwm)
  return -ENOMEM;

 pwm = devm_pwm_get(&pdev->dev, NULL);
 if (IS_ERR(pwm))
  return PTR_ERR(pwm);

 pwm_get_args(pwm, &pargs);
 if (!pargs.period) {
  dev_err(&pdev->dev, "invalid PWM period\n");
  return -EINVAL;
 }

 if (of_property_read_u32(node, "clock-frequency", &clk_pwm->fixed_rate))
  clk_pwm->fixed_rate = div64_u64(NSEC_PER_SEC, pargs.period);

 if (!clk_pwm->fixed_rate) {
  dev_err(&pdev->dev, "fixed_rate cannot be zero\n");
  return -EINVAL;
 }

 if (pargs.period != NSEC_PER_SEC / clk_pwm->fixed_rate &&
     pargs.period != DIV_ROUND_UP(NSEC_PER_SEC, clk_pwm->fixed_rate)) {
  dev_err(&pdev->dev,
   "clock-frequency does not match PWM period\n");
  return -EINVAL;
 }

 pwm_init_state(pwm, &clk_pwm->state);
 pwm_set_relative_duty_cycle(&clk_pwm->state, 1, 2);
 clk_pwm->state.enabled = true;

 clk_name = node->name;
 of_property_read_string(node, "clock-output-names", &clk_name);

 init.name = clk_name;
 if (pwm_might_sleep(pwm))
  init.ops = &clk_pwm_ops;
 else
  init.ops = &clk_pwm_ops_atomic;

 init.flags = 0;
 init.num_parents = 0;

 clk_pwm->pwm = pwm;
 clk_pwm->hw.init = &init;
 ret = devm_clk_hw_register(&pdev->dev, &clk_pwm->hw);
 if (ret)
  return ret;

 return of_clk_add_hw_provider(node, of_clk_hw_simple_get, &clk_pwm->hw);
}

static void clk_pwm_remove(struct platform_device *pdev)
{
 of_clk_del_provider(pdev->dev.of_node);
}

static const struct of_device_id clk_pwm_dt_ids[] = {
 { .compatible = "pwm-clock" },
 { }
};
MODULE_DEVICE_TABLE(of, clk_pwm_dt_ids);

static struct platform_driver clk_pwm_driver = {
 .probe = clk_pwm_probe,
 .remove = clk_pwm_remove,
 .driver = {
  .name = "pwm-clock",
  .of_match_table = clk_pwm_dt_ids,
 },
};

module_platform_driver(clk_pwm_driver);

MODULE_AUTHOR("Philipp Zabel ");
MODULE_DESCRIPTION("PWM clock driver");
MODULE_LICENSE("GPL");

Messung V0.5
C=95 H=91 G=92

¤ Dauer der Verarbeitung: 0.3 Sekunden  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.