Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v4.7 143 lines 3.3 kB view raw
1/* 2 * Copyright (C) 2014 Philipp Zabel, Pengutronix 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License version 2 as 6 * published by the Free Software Foundation. 7 * 8 * PWM (mis)used as clock output 9 */ 10#include <linux/clk-provider.h> 11#include <linux/kernel.h> 12#include <linux/module.h> 13#include <linux/of.h> 14#include <linux/platform_device.h> 15#include <linux/pwm.h> 16 17struct clk_pwm { 18 struct clk_hw hw; 19 struct pwm_device *pwm; 20 u32 fixed_rate; 21}; 22 23static inline struct clk_pwm *to_clk_pwm(struct clk_hw *hw) 24{ 25 return container_of(hw, struct clk_pwm, hw); 26} 27 28static int clk_pwm_prepare(struct clk_hw *hw) 29{ 30 struct clk_pwm *clk_pwm = to_clk_pwm(hw); 31 32 return pwm_enable(clk_pwm->pwm); 33} 34 35static void clk_pwm_unprepare(struct clk_hw *hw) 36{ 37 struct clk_pwm *clk_pwm = to_clk_pwm(hw); 38 39 pwm_disable(clk_pwm->pwm); 40} 41 42static unsigned long clk_pwm_recalc_rate(struct clk_hw *hw, 43 unsigned long parent_rate) 44{ 45 struct clk_pwm *clk_pwm = to_clk_pwm(hw); 46 47 return clk_pwm->fixed_rate; 48} 49 50static const struct clk_ops clk_pwm_ops = { 51 .prepare = clk_pwm_prepare, 52 .unprepare = clk_pwm_unprepare, 53 .recalc_rate = clk_pwm_recalc_rate, 54}; 55 56static int clk_pwm_probe(struct platform_device *pdev) 57{ 58 struct device_node *node = pdev->dev.of_node; 59 struct clk_init_data init; 60 struct clk_pwm *clk_pwm; 61 struct pwm_device *pwm; 62 struct pwm_args pargs; 63 const char *clk_name; 64 struct clk *clk; 65 int ret; 66 67 clk_pwm = devm_kzalloc(&pdev->dev, sizeof(*clk_pwm), GFP_KERNEL); 68 if (!clk_pwm) 69 return -ENOMEM; 70 71 pwm = devm_pwm_get(&pdev->dev, NULL); 72 if (IS_ERR(pwm)) 73 return PTR_ERR(pwm); 74 75 pwm_get_args(pwm, &pargs); 76 if (!pargs.period) { 77 dev_err(&pdev->dev, "invalid PWM period\n"); 78 return -EINVAL; 79 } 80 81 if (of_property_read_u32(node, "clock-frequency", &clk_pwm->fixed_rate)) 82 clk_pwm->fixed_rate = NSEC_PER_SEC / pargs.period; 83 84 if (pargs.period != NSEC_PER_SEC / clk_pwm->fixed_rate && 85 pargs.period != DIV_ROUND_UP(NSEC_PER_SEC, clk_pwm->fixed_rate)) { 86 dev_err(&pdev->dev, 87 "clock-frequency does not match PWM period\n"); 88 return -EINVAL; 89 } 90 91 /* 92 * FIXME: pwm_apply_args() should be removed when switching to the 93 * atomic PWM API. 94 */ 95 pwm_apply_args(pwm); 96 ret = pwm_config(pwm, (pargs.period + 1) >> 1, pargs.period); 97 if (ret < 0) 98 return ret; 99 100 clk_name = node->name; 101 of_property_read_string(node, "clock-output-names", &clk_name); 102 103 init.name = clk_name; 104 init.ops = &clk_pwm_ops; 105 init.flags = CLK_IS_BASIC; 106 init.num_parents = 0; 107 108 clk_pwm->pwm = pwm; 109 clk_pwm->hw.init = &init; 110 clk = devm_clk_register(&pdev->dev, &clk_pwm->hw); 111 if (IS_ERR(clk)) 112 return PTR_ERR(clk); 113 114 return of_clk_add_provider(node, of_clk_src_simple_get, clk); 115} 116 117static int clk_pwm_remove(struct platform_device *pdev) 118{ 119 of_clk_del_provider(pdev->dev.of_node); 120 121 return 0; 122} 123 124static const struct of_device_id clk_pwm_dt_ids[] = { 125 { .compatible = "pwm-clock" }, 126 { } 127}; 128MODULE_DEVICE_TABLE(of, clk_pwm_dt_ids); 129 130static struct platform_driver clk_pwm_driver = { 131 .probe = clk_pwm_probe, 132 .remove = clk_pwm_remove, 133 .driver = { 134 .name = "pwm-clock", 135 .of_match_table = of_match_ptr(clk_pwm_dt_ids), 136 }, 137}; 138 139module_platform_driver(clk_pwm_driver); 140 141MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>"); 142MODULE_DESCRIPTION("PWM clock driver"); 143MODULE_LICENSE("GPL");