blob: da0c1bf2a7054dcc115fd89316c10c5b0cd24aea [file] [log] [blame]
Lee Jones378fe112014-07-14 15:33:27 +01001/*
2 * PWM device driver for ST SoCs.
3 * Author: Ajit Pal Singh <ajitpal.singh@st.com>
4 *
5 * Copyright (C) 2013-2014 STMicroelectronics (R&D) Limited
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
Lee Jones378fe112014-07-14 15:33:27 +010013#include <linux/clk.h>
14#include <linux/math64.h>
15#include <linux/mfd/syscon.h>
16#include <linux/module.h>
17#include <linux/of.h>
18#include <linux/platform_device.h>
19#include <linux/pwm.h>
20#include <linux/regmap.h>
21#include <linux/slab.h>
22#include <linux/time.h>
23
Lee Jonesc5f94ae2016-08-16 10:34:59 +010024#define PWM_OUT_VAL(x) (0x00 + (4 * (x))) /* Device's Duty Cycle register */
25
26#define STI_PWM_CTRL 0x50 /* Control/Config register */
27#define STI_INT_EN 0x54 /* Interrupt Enable/Disable register */
Ajit Pal Singhbf9cc802014-07-14 15:33:29 +010028#define PWM_PRESCALE_LOW_MASK 0x0f
29#define PWM_PRESCALE_HIGH_MASK 0xf0
Lee Jones378fe112014-07-14 15:33:27 +010030
31/* Regfield IDs */
32enum {
Lee Jonesc5f94ae2016-08-16 10:34:59 +010033 /* Bits in PWM_CTRL*/
Ajit Pal Singhbf9cc802014-07-14 15:33:29 +010034 PWMCLK_PRESCALE_LOW,
35 PWMCLK_PRESCALE_HIGH,
Lee Jonesc5f94ae2016-08-16 10:34:59 +010036
37 PWM_OUT_EN,
38
39 PWM_CPT_INT_EN,
Lee Jones378fe112014-07-14 15:33:27 +010040
41 /* Keep last */
42 MAX_REGFIELDS
43};
44
45struct sti_pwm_compat_data {
46 const struct reg_field *reg_fields;
Lee Jones09022e62016-08-16 10:34:58 +010047 unsigned int num_devs;
Lee Jones378fe112014-07-14 15:33:27 +010048 unsigned int max_pwm_cnt;
49 unsigned int max_prescale;
50};
51
52struct sti_pwm_chip {
53 struct device *dev;
Lee Jonesc5f94ae2016-08-16 10:34:59 +010054 struct clk *pwm_clk;
Lee Jones378fe112014-07-14 15:33:27 +010055 struct regmap *regmap;
56 struct sti_pwm_compat_data *cdata;
Ajit Pal Singhbf9cc802014-07-14 15:33:29 +010057 struct regmap_field *prescale_low;
58 struct regmap_field *prescale_high;
Lee Jonesc5f94ae2016-08-16 10:34:59 +010059 struct regmap_field *pwm_out_en;
60 struct regmap_field *pwm_cpt_int_en;
Lee Jones378fe112014-07-14 15:33:27 +010061 struct pwm_chip chip;
Ajit Pal Singh51651662014-07-14 15:33:30 +010062 struct pwm_device *cur;
Ajit Pal Singhcd264b62015-01-29 14:34:43 +053063 unsigned long configured;
Ajit Pal Singh6ad6b832014-07-14 15:33:31 +010064 unsigned int en_count;
65 struct mutex sti_pwm_lock; /* To sync between enable/disable calls */
Lee Jones378fe112014-07-14 15:33:27 +010066 void __iomem *mmio;
67};
68
69static const struct reg_field sti_pwm_regfields[MAX_REGFIELDS] = {
Lee Jonesc5f94ae2016-08-16 10:34:59 +010070 [PWMCLK_PRESCALE_LOW] = REG_FIELD(STI_PWM_CTRL, 0, 3),
71 [PWMCLK_PRESCALE_HIGH] = REG_FIELD(STI_PWM_CTRL, 11, 14),
72 [PWM_OUT_EN] = REG_FIELD(STI_PWM_CTRL, 9, 9),
73 [PWM_CPT_INT_EN] = REG_FIELD(STI_INT_EN, 1, 4),
Lee Jones378fe112014-07-14 15:33:27 +010074};
75
76static inline struct sti_pwm_chip *to_sti_pwmchip(struct pwm_chip *chip)
77{
78 return container_of(chip, struct sti_pwm_chip, chip);
79}
80
81/*
Ajit Pal Singh3aacd3e2014-07-14 15:33:32 +010082 * Calculate the prescaler value corresponding to the period.
Lee Jones378fe112014-07-14 15:33:27 +010083 */
Ajit Pal Singh3aacd3e2014-07-14 15:33:32 +010084static int sti_pwm_get_prescale(struct sti_pwm_chip *pc, unsigned long period,
85 unsigned int *prescale)
Lee Jones378fe112014-07-14 15:33:27 +010086{
87 struct sti_pwm_compat_data *cdata = pc->cdata;
Lee Jonesd81738b2016-08-16 10:35:00 +010088 unsigned long clk_rate;
Lee Jones378fe112014-07-14 15:33:27 +010089 unsigned long val;
Ajit Pal Singh3aacd3e2014-07-14 15:33:32 +010090 unsigned int ps;
Lee Jones378fe112014-07-14 15:33:27 +010091
Lee Jonesd81738b2016-08-16 10:35:00 +010092 clk_rate = clk_get_rate(pc->pwm_clk);
93 if (!clk_rate) {
94 dev_err(pc->dev, "failed to get clock rate\n");
95 return -EINVAL;
96 }
97
Lee Jones378fe112014-07-14 15:33:27 +010098 /*
Ajit Pal Singh3aacd3e2014-07-14 15:33:32 +010099 * prescale = ((period_ns * clk_rate) / (10^9 * (max_pwm_count + 1)) - 1
Lee Jones378fe112014-07-14 15:33:27 +0100100 */
Lee Jonesd81738b2016-08-16 10:35:00 +0100101 val = NSEC_PER_SEC / clk_rate;
Lee Jones378fe112014-07-14 15:33:27 +0100102 val *= cdata->max_pwm_cnt + 1;
103
Ajit Pal Singh3aacd3e2014-07-14 15:33:32 +0100104 if (period % val) {
105 return -EINVAL;
106 } else {
107 ps = period / val - 1;
108 if (ps > cdata->max_prescale)
109 return -EINVAL;
Lee Jones378fe112014-07-14 15:33:27 +0100110 }
Ajit Pal Singh3aacd3e2014-07-14 15:33:32 +0100111 *prescale = ps;
112
113 return 0;
Lee Jones378fe112014-07-14 15:33:27 +0100114}
115
Lee Jones378fe112014-07-14 15:33:27 +0100116/*
117 * For STiH4xx PWM IP, the PWM period is fixed to 256 local clock cycles.
118 * The only way to change the period (apart from changing the PWM input clock)
119 * is to change the PWM clock prescaler.
Ajit Pal Singhbf9cc802014-07-14 15:33:29 +0100120 * The prescaler is of 8 bits, so 256 prescaler values and hence
121 * 256 possible period values are supported (for a particular clock rate).
Lee Jones378fe112014-07-14 15:33:27 +0100122 * The requested period will be applied only if it matches one of these
Ajit Pal Singhbf9cc802014-07-14 15:33:29 +0100123 * 256 values.
Lee Jones378fe112014-07-14 15:33:27 +0100124 */
125static int sti_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
126 int duty_ns, int period_ns)
127{
128 struct sti_pwm_chip *pc = to_sti_pwmchip(chip);
129 struct sti_pwm_compat_data *cdata = pc->cdata;
Ajit Pal Singh51651662014-07-14 15:33:30 +0100130 struct pwm_device *cur = pc->cur;
Lee Jones378fe112014-07-14 15:33:27 +0100131 struct device *dev = pc->dev;
Ajit Pal Singh51651662014-07-14 15:33:30 +0100132 unsigned int prescale = 0, pwmvalx;
Lee Jones378fe112014-07-14 15:33:27 +0100133 int ret;
Ajit Pal Singh51651662014-07-14 15:33:30 +0100134 unsigned int ncfg;
135 bool period_same = false;
Lee Jones378fe112014-07-14 15:33:27 +0100136
Ajit Pal Singhcd264b62015-01-29 14:34:43 +0530137 ncfg = hweight_long(pc->configured);
Ajit Pal Singh51651662014-07-14 15:33:30 +0100138 if (ncfg)
139 period_same = (period_ns == pwm_get_period(cur));
140
141 /* Allow configuration changes if one of the
142 * following conditions satisfy.
Lee Jones09022e62016-08-16 10:34:58 +0100143 * 1. No devices have been configured.
144 * 2. Only one device has been configured and the new request
145 * is for the same device.
146 * 3. Only one device has been configured and the new request is
147 * for a new device and period of the new device is same as
Ajit Pal Singh51651662014-07-14 15:33:30 +0100148 * the current configured period.
Lee Jones09022e62016-08-16 10:34:58 +0100149 * 4. More than one devices are configured and period of the new
Ajit Pal Singh51651662014-07-14 15:33:30 +0100150 * requestis the same as the current period.
Lee Jones378fe112014-07-14 15:33:27 +0100151 */
Ajit Pal Singh51651662014-07-14 15:33:30 +0100152 if (!ncfg ||
153 ((ncfg == 1) && (pwm->hwpwm == cur->hwpwm)) ||
154 ((ncfg == 1) && (pwm->hwpwm != cur->hwpwm) && period_same) ||
155 ((ncfg > 1) && period_same)) {
156 /* Enable clock before writing to PWM registers. */
Lee Jonesc5f94ae2016-08-16 10:34:59 +0100157 ret = clk_enable(pc->pwm_clk);
Ajit Pal Singh51651662014-07-14 15:33:30 +0100158 if (ret)
159 return ret;
160
161 if (!period_same) {
Ajit Pal Singh3aacd3e2014-07-14 15:33:32 +0100162 ret = sti_pwm_get_prescale(pc, period_ns, &prescale);
163 if (ret)
Ajit Pal Singh51651662014-07-14 15:33:30 +0100164 goto clk_dis;
Ajit Pal Singh51651662014-07-14 15:33:30 +0100165
166 ret =
167 regmap_field_write(pc->prescale_low,
168 prescale & PWM_PRESCALE_LOW_MASK);
169 if (ret)
170 goto clk_dis;
171
172 ret =
173 regmap_field_write(pc->prescale_high,
174 (prescale & PWM_PRESCALE_HIGH_MASK) >> 4);
175 if (ret)
176 goto clk_dis;
177 }
178
179 /*
180 * When PWMVal == 0, PWM pulse = 1 local clock cycle.
181 * When PWMVal == max_pwm_count,
182 * PWM pulse = (max_pwm_count + 1) local cycles,
183 * that is continuous pulse: signal never goes low.
184 */
185 pwmvalx = cdata->max_pwm_cnt * duty_ns / period_ns;
186
Lee Jonesc5f94ae2016-08-16 10:34:59 +0100187 ret = regmap_write(pc->regmap,
188 PWM_OUT_VAL(pwm->hwpwm), pwmvalx);
Ajit Pal Singh51651662014-07-14 15:33:30 +0100189 if (ret)
190 goto clk_dis;
191
Lee Jonesc5f94ae2016-08-16 10:34:59 +0100192 ret = regmap_field_write(pc->pwm_cpt_int_en, 0);
Ajit Pal Singh51651662014-07-14 15:33:30 +0100193
Ajit Pal Singhcd264b62015-01-29 14:34:43 +0530194 set_bit(pwm->hwpwm, &pc->configured);
Ajit Pal Singh51651662014-07-14 15:33:30 +0100195 pc->cur = pwm;
196
197 dev_dbg(dev, "prescale:%u, period:%i, duty:%i, pwmvalx:%u\n",
198 prescale, period_ns, duty_ns, pwmvalx);
199 } else {
Lee Jones378fe112014-07-14 15:33:27 +0100200 return -EINVAL;
201 }
202
Lee Jones378fe112014-07-14 15:33:27 +0100203clk_dis:
Lee Jonesc5f94ae2016-08-16 10:34:59 +0100204 clk_disable(pc->pwm_clk);
Lee Jones378fe112014-07-14 15:33:27 +0100205 return ret;
206}
207
208static int sti_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
209{
210 struct sti_pwm_chip *pc = to_sti_pwmchip(chip);
211 struct device *dev = pc->dev;
Ajit Pal Singh6ad6b832014-07-14 15:33:31 +0100212 int ret = 0;
Lee Jones378fe112014-07-14 15:33:27 +0100213
Ajit Pal Singh6ad6b832014-07-14 15:33:31 +0100214 /*
Lee Jones09022e62016-08-16 10:34:58 +0100215 * Since we have a common enable for all PWM devices,
Ajit Pal Singh6ad6b832014-07-14 15:33:31 +0100216 * do not enable if already enabled.
217 */
218 mutex_lock(&pc->sti_pwm_lock);
219 if (!pc->en_count) {
Lee Jonesc5f94ae2016-08-16 10:34:59 +0100220 ret = clk_enable(pc->pwm_clk);
Ajit Pal Singh6ad6b832014-07-14 15:33:31 +0100221 if (ret)
222 goto out;
Lee Jones378fe112014-07-14 15:33:27 +0100223
Lee Jonesc5f94ae2016-08-16 10:34:59 +0100224 ret = regmap_field_write(pc->pwm_out_en, 1);
Ajit Pal Singh6ad6b832014-07-14 15:33:31 +0100225 if (ret) {
226 dev_err(dev, "failed to enable PWM device:%d\n",
227 pwm->hwpwm);
228 goto out;
229 }
230 }
231 pc->en_count++;
232out:
233 mutex_unlock(&pc->sti_pwm_lock);
Lee Jones378fe112014-07-14 15:33:27 +0100234 return ret;
235}
236
237static void sti_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
238{
239 struct sti_pwm_chip *pc = to_sti_pwmchip(chip);
Lee Jones378fe112014-07-14 15:33:27 +0100240
Ajit Pal Singh6ad6b832014-07-14 15:33:31 +0100241 mutex_lock(&pc->sti_pwm_lock);
242 if (--pc->en_count) {
243 mutex_unlock(&pc->sti_pwm_lock);
244 return;
245 }
Lee Jonesc5f94ae2016-08-16 10:34:59 +0100246 regmap_field_write(pc->pwm_out_en, 0);
Lee Jones378fe112014-07-14 15:33:27 +0100247
Lee Jonesc5f94ae2016-08-16 10:34:59 +0100248 clk_disable(pc->pwm_clk);
Ajit Pal Singh6ad6b832014-07-14 15:33:31 +0100249 mutex_unlock(&pc->sti_pwm_lock);
Lee Jones378fe112014-07-14 15:33:27 +0100250}
251
Ajit Pal Singhcd264b62015-01-29 14:34:43 +0530252static void sti_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
253{
254 struct sti_pwm_chip *pc = to_sti_pwmchip(chip);
255
256 clear_bit(pwm->hwpwm, &pc->configured);
257}
258
Lee Jones378fe112014-07-14 15:33:27 +0100259static const struct pwm_ops sti_pwm_ops = {
260 .config = sti_pwm_config,
261 .enable = sti_pwm_enable,
262 .disable = sti_pwm_disable,
Ajit Pal Singhcd264b62015-01-29 14:34:43 +0530263 .free = sti_pwm_free,
Lee Jones378fe112014-07-14 15:33:27 +0100264 .owner = THIS_MODULE,
265};
266
267static int sti_pwm_probe_dt(struct sti_pwm_chip *pc)
268{
269 struct device *dev = pc->dev;
270 const struct reg_field *reg_fields;
271 struct device_node *np = dev->of_node;
272 struct sti_pwm_compat_data *cdata = pc->cdata;
Lee Jones09022e62016-08-16 10:34:58 +0100273 u32 num_devs;
Lee Jones378fe112014-07-14 15:33:27 +0100274
Lee Jones09022e62016-08-16 10:34:58 +0100275 of_property_read_u32(np, "st,pwm-num-chan", &num_devs);
276 if (num_devs)
277 cdata->num_devs = num_devs;
Lee Jones378fe112014-07-14 15:33:27 +0100278
279 reg_fields = cdata->reg_fields;
280
Ajit Pal Singhbf9cc802014-07-14 15:33:29 +0100281 pc->prescale_low = devm_regmap_field_alloc(dev, pc->regmap,
282 reg_fields[PWMCLK_PRESCALE_LOW]);
283 if (IS_ERR(pc->prescale_low))
284 return PTR_ERR(pc->prescale_low);
285
286 pc->prescale_high = devm_regmap_field_alloc(dev, pc->regmap,
287 reg_fields[PWMCLK_PRESCALE_HIGH]);
288 if (IS_ERR(pc->prescale_high))
289 return PTR_ERR(pc->prescale_high);
Lee Jones378fe112014-07-14 15:33:27 +0100290
Lee Jones378fe112014-07-14 15:33:27 +0100291
Lee Jonesc5f94ae2016-08-16 10:34:59 +0100292 pc->pwm_out_en = devm_regmap_field_alloc(dev, pc->regmap,
293 reg_fields[PWM_OUT_EN]);
294 if (IS_ERR(pc->pwm_out_en))
295 return PTR_ERR(pc->pwm_out_en);
296
297 pc->pwm_cpt_int_en = devm_regmap_field_alloc(dev, pc->regmap,
298 reg_fields[PWM_CPT_INT_EN]);
299 if (IS_ERR(pc->pwm_cpt_int_en))
300 return PTR_ERR(pc->pwm_cpt_int_en);
Lee Jones378fe112014-07-14 15:33:27 +0100301
302 return 0;
303}
304
305static const struct regmap_config sti_pwm_regmap_config = {
306 .reg_bits = 32,
307 .val_bits = 32,
308 .reg_stride = 4,
309};
310
311static int sti_pwm_probe(struct platform_device *pdev)
312{
313 struct device *dev = &pdev->dev;
314 struct sti_pwm_compat_data *cdata;
315 struct sti_pwm_chip *pc;
316 struct resource *res;
317 int ret;
318
319 pc = devm_kzalloc(dev, sizeof(*pc), GFP_KERNEL);
320 if (!pc)
321 return -ENOMEM;
322
323 cdata = devm_kzalloc(dev, sizeof(*cdata), GFP_KERNEL);
324 if (!cdata)
325 return -ENOMEM;
326
327 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
328
329 pc->mmio = devm_ioremap_resource(dev, res);
330 if (IS_ERR(pc->mmio))
331 return PTR_ERR(pc->mmio);
332
333 pc->regmap = devm_regmap_init_mmio(dev, pc->mmio,
334 &sti_pwm_regmap_config);
335 if (IS_ERR(pc->regmap))
336 return PTR_ERR(pc->regmap);
337
338 /*
339 * Setup PWM data with default values: some values could be replaced
340 * with specific ones provided from Device Tree.
341 */
342 cdata->reg_fields = &sti_pwm_regfields[0];
343 cdata->max_prescale = 0xff;
344 cdata->max_pwm_cnt = 255;
Lee Jones09022e62016-08-16 10:34:58 +0100345 cdata->num_devs = 1;
Lee Jones378fe112014-07-14 15:33:27 +0100346
347 pc->cdata = cdata;
348 pc->dev = dev;
Ajit Pal Singh6ad6b832014-07-14 15:33:31 +0100349 pc->en_count = 0;
350 mutex_init(&pc->sti_pwm_lock);
Lee Jones378fe112014-07-14 15:33:27 +0100351
352 ret = sti_pwm_probe_dt(pc);
353 if (ret)
354 return ret;
355
Lee Jonesc5f94ae2016-08-16 10:34:59 +0100356 pc->pwm_clk = of_clk_get_by_name(dev->of_node, "pwm");
357 if (IS_ERR(pc->pwm_clk)) {
Lee Jones378fe112014-07-14 15:33:27 +0100358 dev_err(dev, "failed to get PWM clock\n");
Lee Jonesc5f94ae2016-08-16 10:34:59 +0100359 return PTR_ERR(pc->pwm_clk);
Lee Jones378fe112014-07-14 15:33:27 +0100360 }
361
Lee Jonesc5f94ae2016-08-16 10:34:59 +0100362 ret = clk_prepare(pc->pwm_clk);
Lee Jones378fe112014-07-14 15:33:27 +0100363 if (ret) {
364 dev_err(dev, "failed to prepare clock\n");
365 return ret;
366 }
367
Lee Jones378fe112014-07-14 15:33:27 +0100368 pc->chip.dev = dev;
369 pc->chip.ops = &sti_pwm_ops;
370 pc->chip.base = -1;
Lee Jones09022e62016-08-16 10:34:58 +0100371 pc->chip.npwm = pc->cdata->num_devs;
Lee Jones378fe112014-07-14 15:33:27 +0100372 pc->chip.can_sleep = true;
373
374 ret = pwmchip_add(&pc->chip);
375 if (ret < 0) {
Lee Jonesc5f94ae2016-08-16 10:34:59 +0100376 clk_unprepare(pc->pwm_clk);
Lee Jones378fe112014-07-14 15:33:27 +0100377 return ret;
378 }
379
380 platform_set_drvdata(pdev, pc);
381
382 return 0;
383}
384
385static int sti_pwm_remove(struct platform_device *pdev)
386{
387 struct sti_pwm_chip *pc = platform_get_drvdata(pdev);
388 unsigned int i;
389
Lee Jones09022e62016-08-16 10:34:58 +0100390 for (i = 0; i < pc->cdata->num_devs; i++)
Lee Jones378fe112014-07-14 15:33:27 +0100391 pwm_disable(&pc->chip.pwms[i]);
392
Lee Jonesc5f94ae2016-08-16 10:34:59 +0100393 clk_unprepare(pc->pwm_clk);
Lee Jones378fe112014-07-14 15:33:27 +0100394
395 return pwmchip_remove(&pc->chip);
396}
397
398static const struct of_device_id sti_pwm_of_match[] = {
399 { .compatible = "st,sti-pwm", },
400 { /* sentinel */ }
401};
402MODULE_DEVICE_TABLE(of, sti_pwm_of_match);
403
404static struct platform_driver sti_pwm_driver = {
405 .driver = {
406 .name = "sti-pwm",
407 .of_match_table = sti_pwm_of_match,
408 },
409 .probe = sti_pwm_probe,
410 .remove = sti_pwm_remove,
411};
412module_platform_driver(sti_pwm_driver);
413
414MODULE_AUTHOR("Ajit Pal Singh <ajitpal.singh@st.com>");
415MODULE_DESCRIPTION("STMicroelectronics ST PWM driver");
416MODULE_LICENSE("GPL");