1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) STMicroelectronics 2019 - All Rights Reserved
4*4882a593Smuzhiyun * Authors: Benjamin Gaignard <benjamin.gaignard@st.com> for STMicroelectronics.
5*4882a593Smuzhiyun * Pascal Paillet <p.paillet@st.com> for STMicroelectronics.
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/clk.h>
9*4882a593Smuzhiyun #include <linux/clockchips.h>
10*4882a593Smuzhiyun #include <linux/interrupt.h>
11*4882a593Smuzhiyun #include <linux/mfd/stm32-lptimer.h>
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <linux/of_address.h>
14*4882a593Smuzhiyun #include <linux/of_irq.h>
15*4882a593Smuzhiyun #include <linux/platform_device.h>
16*4882a593Smuzhiyun #include <linux/pm_wakeirq.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #define CFGR_PSC_OFFSET 9
19*4882a593Smuzhiyun #define STM32_LP_RATING 1000
20*4882a593Smuzhiyun #define STM32_TARGET_CLKRATE (32000 * HZ)
21*4882a593Smuzhiyun #define STM32_LP_MAX_PSC 7
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun struct stm32_lp_private {
24*4882a593Smuzhiyun struct regmap *reg;
25*4882a593Smuzhiyun struct clock_event_device clkevt;
26*4882a593Smuzhiyun unsigned long period;
27*4882a593Smuzhiyun struct device *dev;
28*4882a593Smuzhiyun };
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun static struct stm32_lp_private*
to_priv(struct clock_event_device * clkevt)31*4882a593Smuzhiyun to_priv(struct clock_event_device *clkevt)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun return container_of(clkevt, struct stm32_lp_private, clkevt);
34*4882a593Smuzhiyun }
35*4882a593Smuzhiyun
stm32_clkevent_lp_shutdown(struct clock_event_device * clkevt)36*4882a593Smuzhiyun static int stm32_clkevent_lp_shutdown(struct clock_event_device *clkevt)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun struct stm32_lp_private *priv = to_priv(clkevt);
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun regmap_write(priv->reg, STM32_LPTIM_CR, 0);
41*4882a593Smuzhiyun regmap_write(priv->reg, STM32_LPTIM_IER, 0);
42*4882a593Smuzhiyun /* clear pending flags */
43*4882a593Smuzhiyun regmap_write(priv->reg, STM32_LPTIM_ICR, STM32_LPTIM_ARRMCF);
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun return 0;
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun
stm32_clkevent_lp_set_timer(unsigned long evt,struct clock_event_device * clkevt,int is_periodic)48*4882a593Smuzhiyun static int stm32_clkevent_lp_set_timer(unsigned long evt,
49*4882a593Smuzhiyun struct clock_event_device *clkevt,
50*4882a593Smuzhiyun int is_periodic)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun struct stm32_lp_private *priv = to_priv(clkevt);
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /* disable LPTIMER to be able to write into IER register*/
55*4882a593Smuzhiyun regmap_write(priv->reg, STM32_LPTIM_CR, 0);
56*4882a593Smuzhiyun /* enable ARR interrupt */
57*4882a593Smuzhiyun regmap_write(priv->reg, STM32_LPTIM_IER, STM32_LPTIM_ARRMIE);
58*4882a593Smuzhiyun /* enable LPTIMER to be able to write into ARR register */
59*4882a593Smuzhiyun regmap_write(priv->reg, STM32_LPTIM_CR, STM32_LPTIM_ENABLE);
60*4882a593Smuzhiyun /* set next event counter */
61*4882a593Smuzhiyun regmap_write(priv->reg, STM32_LPTIM_ARR, evt);
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun /* start counter */
64*4882a593Smuzhiyun if (is_periodic)
65*4882a593Smuzhiyun regmap_write(priv->reg, STM32_LPTIM_CR,
66*4882a593Smuzhiyun STM32_LPTIM_CNTSTRT | STM32_LPTIM_ENABLE);
67*4882a593Smuzhiyun else
68*4882a593Smuzhiyun regmap_write(priv->reg, STM32_LPTIM_CR,
69*4882a593Smuzhiyun STM32_LPTIM_SNGSTRT | STM32_LPTIM_ENABLE);
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun return 0;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
stm32_clkevent_lp_set_next_event(unsigned long evt,struct clock_event_device * clkevt)74*4882a593Smuzhiyun static int stm32_clkevent_lp_set_next_event(unsigned long evt,
75*4882a593Smuzhiyun struct clock_event_device *clkevt)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun return stm32_clkevent_lp_set_timer(evt, clkevt,
78*4882a593Smuzhiyun clockevent_state_periodic(clkevt));
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
stm32_clkevent_lp_set_periodic(struct clock_event_device * clkevt)81*4882a593Smuzhiyun static int stm32_clkevent_lp_set_periodic(struct clock_event_device *clkevt)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun struct stm32_lp_private *priv = to_priv(clkevt);
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun return stm32_clkevent_lp_set_timer(priv->period, clkevt, true);
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun
stm32_clkevent_lp_set_oneshot(struct clock_event_device * clkevt)88*4882a593Smuzhiyun static int stm32_clkevent_lp_set_oneshot(struct clock_event_device *clkevt)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun struct stm32_lp_private *priv = to_priv(clkevt);
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun return stm32_clkevent_lp_set_timer(priv->period, clkevt, false);
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun
stm32_clkevent_lp_irq_handler(int irq,void * dev_id)95*4882a593Smuzhiyun static irqreturn_t stm32_clkevent_lp_irq_handler(int irq, void *dev_id)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun struct clock_event_device *clkevt = (struct clock_event_device *)dev_id;
98*4882a593Smuzhiyun struct stm32_lp_private *priv = to_priv(clkevt);
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun regmap_write(priv->reg, STM32_LPTIM_ICR, STM32_LPTIM_ARRMCF);
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun if (clkevt->event_handler)
103*4882a593Smuzhiyun clkevt->event_handler(clkevt);
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun return IRQ_HANDLED;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
stm32_clkevent_lp_set_prescaler(struct stm32_lp_private * priv,unsigned long * rate)108*4882a593Smuzhiyun static void stm32_clkevent_lp_set_prescaler(struct stm32_lp_private *priv,
109*4882a593Smuzhiyun unsigned long *rate)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun int i;
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun for (i = 0; i <= STM32_LP_MAX_PSC; i++) {
114*4882a593Smuzhiyun if (DIV_ROUND_CLOSEST(*rate, 1 << i) < STM32_TARGET_CLKRATE)
115*4882a593Smuzhiyun break;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun regmap_write(priv->reg, STM32_LPTIM_CFGR, i << CFGR_PSC_OFFSET);
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun /* Adjust rate and period given the prescaler value */
121*4882a593Smuzhiyun *rate = DIV_ROUND_CLOSEST(*rate, (1 << i));
122*4882a593Smuzhiyun priv->period = DIV_ROUND_UP(*rate, HZ);
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun
stm32_clkevent_lp_init(struct stm32_lp_private * priv,struct device_node * np,unsigned long rate)125*4882a593Smuzhiyun static void stm32_clkevent_lp_init(struct stm32_lp_private *priv,
126*4882a593Smuzhiyun struct device_node *np, unsigned long rate)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun priv->clkevt.name = np->full_name;
129*4882a593Smuzhiyun priv->clkevt.cpumask = cpu_possible_mask;
130*4882a593Smuzhiyun priv->clkevt.features = CLOCK_EVT_FEAT_PERIODIC |
131*4882a593Smuzhiyun CLOCK_EVT_FEAT_ONESHOT;
132*4882a593Smuzhiyun priv->clkevt.set_state_shutdown = stm32_clkevent_lp_shutdown;
133*4882a593Smuzhiyun priv->clkevt.set_state_periodic = stm32_clkevent_lp_set_periodic;
134*4882a593Smuzhiyun priv->clkevt.set_state_oneshot = stm32_clkevent_lp_set_oneshot;
135*4882a593Smuzhiyun priv->clkevt.set_next_event = stm32_clkevent_lp_set_next_event;
136*4882a593Smuzhiyun priv->clkevt.rating = STM32_LP_RATING;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun clockevents_config_and_register(&priv->clkevt, rate, 0x1,
139*4882a593Smuzhiyun STM32_LPTIM_MAX_ARR);
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun
stm32_clkevent_lp_probe(struct platform_device * pdev)142*4882a593Smuzhiyun static int stm32_clkevent_lp_probe(struct platform_device *pdev)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun struct stm32_lptimer *ddata = dev_get_drvdata(pdev->dev.parent);
145*4882a593Smuzhiyun struct stm32_lp_private *priv;
146*4882a593Smuzhiyun unsigned long rate;
147*4882a593Smuzhiyun int ret, irq;
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
150*4882a593Smuzhiyun if (!priv)
151*4882a593Smuzhiyun return -ENOMEM;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun priv->reg = ddata->regmap;
154*4882a593Smuzhiyun ret = clk_prepare_enable(ddata->clk);
155*4882a593Smuzhiyun if (ret)
156*4882a593Smuzhiyun return -EINVAL;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun rate = clk_get_rate(ddata->clk);
159*4882a593Smuzhiyun if (!rate) {
160*4882a593Smuzhiyun ret = -EINVAL;
161*4882a593Smuzhiyun goto out_clk_disable;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun irq = platform_get_irq(to_platform_device(pdev->dev.parent), 0);
165*4882a593Smuzhiyun if (irq <= 0) {
166*4882a593Smuzhiyun ret = irq;
167*4882a593Smuzhiyun goto out_clk_disable;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun if (of_property_read_bool(pdev->dev.parent->of_node, "wakeup-source")) {
171*4882a593Smuzhiyun ret = device_init_wakeup(&pdev->dev, true);
172*4882a593Smuzhiyun if (ret)
173*4882a593Smuzhiyun goto out_clk_disable;
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun ret = dev_pm_set_wake_irq(&pdev->dev, irq);
176*4882a593Smuzhiyun if (ret)
177*4882a593Smuzhiyun goto out_clk_disable;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun ret = devm_request_irq(&pdev->dev, irq, stm32_clkevent_lp_irq_handler,
181*4882a593Smuzhiyun IRQF_TIMER, pdev->name, &priv->clkevt);
182*4882a593Smuzhiyun if (ret)
183*4882a593Smuzhiyun goto out_clk_disable;
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun stm32_clkevent_lp_set_prescaler(priv, &rate);
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun stm32_clkevent_lp_init(priv, pdev->dev.parent->of_node, rate);
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun priv->dev = &pdev->dev;
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun return 0;
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun out_clk_disable:
194*4882a593Smuzhiyun clk_disable_unprepare(ddata->clk);
195*4882a593Smuzhiyun return ret;
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun
stm32_clkevent_lp_remove(struct platform_device * pdev)198*4882a593Smuzhiyun static int stm32_clkevent_lp_remove(struct platform_device *pdev)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun return -EBUSY; /* cannot unregister clockevent */
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun static const struct of_device_id stm32_clkevent_lp_of_match[] = {
204*4882a593Smuzhiyun { .compatible = "st,stm32-lptimer-timer", },
205*4882a593Smuzhiyun {},
206*4882a593Smuzhiyun };
207*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, stm32_clkevent_lp_of_match);
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun static struct platform_driver stm32_clkevent_lp_driver = {
210*4882a593Smuzhiyun .probe = stm32_clkevent_lp_probe,
211*4882a593Smuzhiyun .remove = stm32_clkevent_lp_remove,
212*4882a593Smuzhiyun .driver = {
213*4882a593Smuzhiyun .name = "stm32-lptimer-timer",
214*4882a593Smuzhiyun .of_match_table = of_match_ptr(stm32_clkevent_lp_of_match),
215*4882a593Smuzhiyun },
216*4882a593Smuzhiyun };
217*4882a593Smuzhiyun module_platform_driver(stm32_clkevent_lp_driver);
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun MODULE_ALIAS("platform:stm32-lptimer-timer");
220*4882a593Smuzhiyun MODULE_DESCRIPTION("STMicroelectronics STM32 clockevent low power driver");
221*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
222