Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Imagination Technologies Pulse Width Modulator driver
  4 *
  5 * Copyright (c) 2014-2015, Imagination Technologies
  6 *
  7 * Based on drivers/pwm/pwm-tegra.c, Copyright (c) 2010, NVIDIA Corporation
 
 
 
 
  8 */
  9
 10#include <linux/clk.h>
 11#include <linux/err.h>
 12#include <linux/io.h>
 13#include <linux/mfd/syscon.h>
 14#include <linux/module.h>
 15#include <linux/of.h>
 
 16#include <linux/platform_device.h>
 17#include <linux/pm_runtime.h>
 18#include <linux/property.h>
 19#include <linux/pwm.h>
 20#include <linux/regmap.h>
 21#include <linux/slab.h>
 22
 23/* PWM registers */
 24#define PWM_CTRL_CFG				0x0000
 25#define PWM_CTRL_CFG_NO_SUB_DIV			0
 26#define PWM_CTRL_CFG_SUB_DIV0			1
 27#define PWM_CTRL_CFG_SUB_DIV1			2
 28#define PWM_CTRL_CFG_SUB_DIV0_DIV1		3
 29#define PWM_CTRL_CFG_DIV_SHIFT(ch)		((ch) * 2 + 4)
 30#define PWM_CTRL_CFG_DIV_MASK			0x3
 31
 32#define PWM_CH_CFG(ch)				(0x4 + (ch) * 4)
 33#define PWM_CH_CFG_TMBASE_SHIFT			0
 34#define PWM_CH_CFG_DUTY_SHIFT			16
 35
 36#define PERIP_PWM_PDM_CONTROL			0x0140
 37#define PERIP_PWM_PDM_CONTROL_CH_MASK		0x1
 38#define PERIP_PWM_PDM_CONTROL_CH_SHIFT(ch)	((ch) * 4)
 39
 40#define IMG_PWM_PM_TIMEOUT			1000 /* ms */
 41
 42/*
 43 * PWM period is specified with a timebase register,
 44 * in number of step periods. The PWM duty cycle is also
 45 * specified in step periods, in the [0, $timebase] range.
 46 * In other words, the timebase imposes the duty cycle
 47 * resolution. Therefore, let's constraint the timebase to
 48 * a minimum value to allow a sane range of duty cycle values.
 49 * Imposing a minimum timebase, will impose a maximum PWM frequency.
 50 *
 51 * The value chosen is completely arbitrary.
 52 */
 53#define MIN_TMBASE_STEPS			16
 54
 55#define IMG_PWM_NPWM				4
 56
 57struct img_pwm_soc_data {
 58	u32 max_timebase;
 59};
 60
 61struct img_pwm_chip {
 62	struct device	*dev;
 63	struct pwm_chip	chip;
 64	struct clk	*pwm_clk;
 65	struct clk	*sys_clk;
 66	void __iomem	*base;
 67	struct regmap	*periph_regs;
 68	int		max_period_ns;
 69	int		min_period_ns;
 70	const struct img_pwm_soc_data   *data;
 71	u32		suspend_ctrl_cfg;
 72	u32		suspend_ch_cfg[IMG_PWM_NPWM];
 73};
 74
 75static inline struct img_pwm_chip *to_img_pwm_chip(struct pwm_chip *chip)
 76{
 77	return container_of(chip, struct img_pwm_chip, chip);
 78}
 79
 80static inline void img_pwm_writel(struct img_pwm_chip *imgchip,
 81				  u32 reg, u32 val)
 82{
 83	writel(val, imgchip->base + reg);
 84}
 85
 86static inline u32 img_pwm_readl(struct img_pwm_chip *imgchip, u32 reg)
 
 87{
 88	return readl(imgchip->base + reg);
 89}
 90
 91static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 92			  int duty_ns, int period_ns)
 93{
 94	u32 val, div, duty, timebase;
 95	unsigned long mul, output_clk_hz, input_clk_hz;
 96	struct img_pwm_chip *imgchip = to_img_pwm_chip(chip);
 97	unsigned int max_timebase = imgchip->data->max_timebase;
 98	int ret;
 99
100	if (period_ns < imgchip->min_period_ns ||
101	    period_ns > imgchip->max_period_ns) {
102		dev_err(chip->dev, "configured period not in range\n");
103		return -ERANGE;
104	}
105
106	input_clk_hz = clk_get_rate(imgchip->pwm_clk);
107	output_clk_hz = DIV_ROUND_UP(NSEC_PER_SEC, period_ns);
108
109	mul = DIV_ROUND_UP(input_clk_hz, output_clk_hz);
110	if (mul <= max_timebase) {
111		div = PWM_CTRL_CFG_NO_SUB_DIV;
112		timebase = DIV_ROUND_UP(mul, 1);
113	} else if (mul <= max_timebase * 8) {
114		div = PWM_CTRL_CFG_SUB_DIV0;
115		timebase = DIV_ROUND_UP(mul, 8);
116	} else if (mul <= max_timebase * 64) {
117		div = PWM_CTRL_CFG_SUB_DIV1;
118		timebase = DIV_ROUND_UP(mul, 64);
119	} else if (mul <= max_timebase * 512) {
120		div = PWM_CTRL_CFG_SUB_DIV0_DIV1;
121		timebase = DIV_ROUND_UP(mul, 512);
122	} else {
123		dev_err(chip->dev,
124			"failed to configure timebase steps/divider value\n");
125		return -EINVAL;
126	}
127
128	duty = DIV_ROUND_UP(timebase * duty_ns, period_ns);
129
130	ret = pm_runtime_resume_and_get(chip->dev);
131	if (ret < 0)
132		return ret;
133
134	val = img_pwm_readl(imgchip, PWM_CTRL_CFG);
135	val &= ~(PWM_CTRL_CFG_DIV_MASK << PWM_CTRL_CFG_DIV_SHIFT(pwm->hwpwm));
136	val |= (div & PWM_CTRL_CFG_DIV_MASK) <<
137		PWM_CTRL_CFG_DIV_SHIFT(pwm->hwpwm);
138	img_pwm_writel(imgchip, PWM_CTRL_CFG, val);
139
140	val = (duty << PWM_CH_CFG_DUTY_SHIFT) |
141	      (timebase << PWM_CH_CFG_TMBASE_SHIFT);
142	img_pwm_writel(imgchip, PWM_CH_CFG(pwm->hwpwm), val);
143
144	pm_runtime_mark_last_busy(chip->dev);
145	pm_runtime_put_autosuspend(chip->dev);
146
147	return 0;
148}
149
150static int img_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
151{
152	u32 val;
153	struct img_pwm_chip *imgchip = to_img_pwm_chip(chip);
154	int ret;
155
156	ret = pm_runtime_resume_and_get(chip->dev);
157	if (ret < 0)
158		return ret;
159
160	val = img_pwm_readl(imgchip, PWM_CTRL_CFG);
161	val |= BIT(pwm->hwpwm);
162	img_pwm_writel(imgchip, PWM_CTRL_CFG, val);
163
164	regmap_clear_bits(imgchip->periph_regs, PERIP_PWM_PDM_CONTROL,
165			  PERIP_PWM_PDM_CONTROL_CH_MASK <<
166			  PERIP_PWM_PDM_CONTROL_CH_SHIFT(pwm->hwpwm));
167
168	return 0;
169}
170
171static void img_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
172{
173	u32 val;
174	struct img_pwm_chip *imgchip = to_img_pwm_chip(chip);
175
176	val = img_pwm_readl(imgchip, PWM_CTRL_CFG);
177	val &= ~BIT(pwm->hwpwm);
178	img_pwm_writel(imgchip, PWM_CTRL_CFG, val);
179
180	pm_runtime_mark_last_busy(chip->dev);
181	pm_runtime_put_autosuspend(chip->dev);
182}
183
184static int img_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
185			 const struct pwm_state *state)
186{
187	int err;
188
189	if (state->polarity != PWM_POLARITY_NORMAL)
190		return -EINVAL;
191
192	if (!state->enabled) {
193		if (pwm->state.enabled)
194			img_pwm_disable(chip, pwm);
195
196		return 0;
197	}
198
199	err = img_pwm_config(chip, pwm, state->duty_cycle, state->period);
200	if (err)
201		return err;
202
203	if (!pwm->state.enabled)
204		err = img_pwm_enable(chip, pwm);
205
206	return err;
207}
208
209static const struct pwm_ops img_pwm_ops = {
210	.apply = img_pwm_apply,
 
 
 
211};
212
213static const struct img_pwm_soc_data pistachio_pwm = {
214	.max_timebase = 255,
215};
216
217static const struct of_device_id img_pwm_of_match[] = {
218	{
219		.compatible = "img,pistachio-pwm",
220		.data = &pistachio_pwm,
221	},
222	{ }
223};
224MODULE_DEVICE_TABLE(of, img_pwm_of_match);
225
226static int img_pwm_runtime_suspend(struct device *dev)
227{
228	struct img_pwm_chip *imgchip = dev_get_drvdata(dev);
229
230	clk_disable_unprepare(imgchip->pwm_clk);
231	clk_disable_unprepare(imgchip->sys_clk);
232
233	return 0;
234}
235
236static int img_pwm_runtime_resume(struct device *dev)
237{
238	struct img_pwm_chip *imgchip = dev_get_drvdata(dev);
239	int ret;
240
241	ret = clk_prepare_enable(imgchip->sys_clk);
242	if (ret < 0) {
243		dev_err(dev, "could not prepare or enable sys clock\n");
244		return ret;
245	}
246
247	ret = clk_prepare_enable(imgchip->pwm_clk);
248	if (ret < 0) {
249		dev_err(dev, "could not prepare or enable pwm clock\n");
250		clk_disable_unprepare(imgchip->sys_clk);
251		return ret;
252	}
253
254	return 0;
255}
256
257static int img_pwm_probe(struct platform_device *pdev)
258{
259	int ret;
260	u64 val;
261	unsigned long clk_rate;
262	struct img_pwm_chip *imgchip;
 
 
263
264	imgchip = devm_kzalloc(&pdev->dev, sizeof(*imgchip), GFP_KERNEL);
265	if (!imgchip)
266		return -ENOMEM;
267
268	imgchip->dev = &pdev->dev;
269
270	imgchip->base = devm_platform_ioremap_resource(pdev, 0);
271	if (IS_ERR(imgchip->base))
272		return PTR_ERR(imgchip->base);
273
274	imgchip->data = device_get_match_data(&pdev->dev);
275
276	imgchip->periph_regs = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
277							       "img,cr-periph");
278	if (IS_ERR(imgchip->periph_regs))
279		return PTR_ERR(imgchip->periph_regs);
 
 
 
 
 
 
 
 
 
 
280
281	imgchip->sys_clk = devm_clk_get(&pdev->dev, "sys");
282	if (IS_ERR(imgchip->sys_clk)) {
283		dev_err(&pdev->dev, "failed to get system clock\n");
284		return PTR_ERR(imgchip->sys_clk);
285	}
286
287	imgchip->pwm_clk = devm_clk_get(&pdev->dev, "imgchip");
288	if (IS_ERR(imgchip->pwm_clk)) {
289		dev_err(&pdev->dev, "failed to get imgchip clock\n");
290		return PTR_ERR(imgchip->pwm_clk);
291	}
292
293	platform_set_drvdata(pdev, imgchip);
294
295	pm_runtime_set_autosuspend_delay(&pdev->dev, IMG_PWM_PM_TIMEOUT);
296	pm_runtime_use_autosuspend(&pdev->dev);
297	pm_runtime_enable(&pdev->dev);
298	if (!pm_runtime_enabled(&pdev->dev)) {
299		ret = img_pwm_runtime_resume(&pdev->dev);
300		if (ret)
301			goto err_pm_disable;
302	}
303
304	clk_rate = clk_get_rate(imgchip->pwm_clk);
305	if (!clk_rate) {
306		dev_err(&pdev->dev, "imgchip clock has no frequency\n");
307		ret = -EINVAL;
308		goto err_suspend;
309	}
310
311	/* The maximum input clock divider is 512 */
312	val = (u64)NSEC_PER_SEC * 512 * imgchip->data->max_timebase;
313	do_div(val, clk_rate);
314	imgchip->max_period_ns = val;
315
316	val = (u64)NSEC_PER_SEC * MIN_TMBASE_STEPS;
317	do_div(val, clk_rate);
318	imgchip->min_period_ns = val;
319
320	imgchip->chip.dev = &pdev->dev;
321	imgchip->chip.ops = &img_pwm_ops;
322	imgchip->chip.npwm = IMG_PWM_NPWM;
 
323
324	ret = pwmchip_add(&imgchip->chip);
325	if (ret < 0) {
326		dev_err(&pdev->dev, "pwmchip_add failed: %d\n", ret);
327		goto err_suspend;
328	}
329
 
330	return 0;
331
332err_suspend:
333	if (!pm_runtime_enabled(&pdev->dev))
334		img_pwm_runtime_suspend(&pdev->dev);
335err_pm_disable:
336	pm_runtime_disable(&pdev->dev);
337	pm_runtime_dont_use_autosuspend(&pdev->dev);
338	return ret;
339}
340
341static void img_pwm_remove(struct platform_device *pdev)
342{
343	struct img_pwm_chip *imgchip = platform_get_drvdata(pdev);
 
 
 
 
 
 
 
 
 
 
 
 
 
344
 
345	pm_runtime_disable(&pdev->dev);
346	if (!pm_runtime_status_suspended(&pdev->dev))
347		img_pwm_runtime_suspend(&pdev->dev);
348
349	pwmchip_remove(&imgchip->chip);
350}
351
352#ifdef CONFIG_PM_SLEEP
353static int img_pwm_suspend(struct device *dev)
354{
355	struct img_pwm_chip *imgchip = dev_get_drvdata(dev);
356	int i, ret;
357
358	if (pm_runtime_status_suspended(dev)) {
359		ret = img_pwm_runtime_resume(dev);
360		if (ret)
361			return ret;
362	}
363
364	for (i = 0; i < imgchip->chip.npwm; i++)
365		imgchip->suspend_ch_cfg[i] = img_pwm_readl(imgchip,
366							   PWM_CH_CFG(i));
367
368	imgchip->suspend_ctrl_cfg = img_pwm_readl(imgchip, PWM_CTRL_CFG);
369
370	img_pwm_runtime_suspend(dev);
371
372	return 0;
373}
374
375static int img_pwm_resume(struct device *dev)
376{
377	struct img_pwm_chip *imgchip = dev_get_drvdata(dev);
378	int ret;
379	int i;
380
381	ret = img_pwm_runtime_resume(dev);
382	if (ret)
383		return ret;
384
385	for (i = 0; i < imgchip->chip.npwm; i++)
386		img_pwm_writel(imgchip, PWM_CH_CFG(i),
387			       imgchip->suspend_ch_cfg[i]);
388
389	img_pwm_writel(imgchip, PWM_CTRL_CFG, imgchip->suspend_ctrl_cfg);
390
391	for (i = 0; i < imgchip->chip.npwm; i++)
392		if (imgchip->suspend_ctrl_cfg & BIT(i))
393			regmap_clear_bits(imgchip->periph_regs,
394					  PERIP_PWM_PDM_CONTROL,
395					  PERIP_PWM_PDM_CONTROL_CH_MASK <<
396					  PERIP_PWM_PDM_CONTROL_CH_SHIFT(i));
 
397
398	if (pm_runtime_status_suspended(dev))
399		img_pwm_runtime_suspend(dev);
400
401	return 0;
402}
403#endif /* CONFIG_PM */
404
405static const struct dev_pm_ops img_pwm_pm_ops = {
406	SET_RUNTIME_PM_OPS(img_pwm_runtime_suspend,
407			   img_pwm_runtime_resume,
408			   NULL)
409	SET_SYSTEM_SLEEP_PM_OPS(img_pwm_suspend, img_pwm_resume)
410};
411
412static struct platform_driver img_pwm_driver = {
413	.driver = {
414		.name = "img-pwm",
415		.pm = &img_pwm_pm_ops,
416		.of_match_table = img_pwm_of_match,
417	},
418	.probe = img_pwm_probe,
419	.remove_new = img_pwm_remove,
420};
421module_platform_driver(img_pwm_driver);
422
423MODULE_AUTHOR("Sai Masarapu <Sai.Masarapu@imgtec.com>");
424MODULE_DESCRIPTION("Imagination Technologies PWM DAC driver");
425MODULE_LICENSE("GPL v2");
v4.17
 
  1/*
  2 * Imagination Technologies Pulse Width Modulator driver
  3 *
  4 * Copyright (c) 2014-2015, Imagination Technologies
  5 *
  6 * Based on drivers/pwm/pwm-tegra.c, Copyright (c) 2010, NVIDIA Corporation
  7 *
  8 * This program is free software; you can redistribute it and/or modify
  9 * it under the terms of the GNU General Public License as published by
 10 * the Free Software Foundation; either version 2 of the License.
 11 */
 12
 13#include <linux/clk.h>
 14#include <linux/err.h>
 15#include <linux/io.h>
 16#include <linux/mfd/syscon.h>
 17#include <linux/module.h>
 18#include <linux/of.h>
 19#include <linux/of_device.h>
 20#include <linux/platform_device.h>
 21#include <linux/pm_runtime.h>
 
 22#include <linux/pwm.h>
 23#include <linux/regmap.h>
 24#include <linux/slab.h>
 25
 26/* PWM registers */
 27#define PWM_CTRL_CFG				0x0000
 28#define PWM_CTRL_CFG_NO_SUB_DIV			0
 29#define PWM_CTRL_CFG_SUB_DIV0			1
 30#define PWM_CTRL_CFG_SUB_DIV1			2
 31#define PWM_CTRL_CFG_SUB_DIV0_DIV1		3
 32#define PWM_CTRL_CFG_DIV_SHIFT(ch)		((ch) * 2 + 4)
 33#define PWM_CTRL_CFG_DIV_MASK			0x3
 34
 35#define PWM_CH_CFG(ch)				(0x4 + (ch) * 4)
 36#define PWM_CH_CFG_TMBASE_SHIFT			0
 37#define PWM_CH_CFG_DUTY_SHIFT			16
 38
 39#define PERIP_PWM_PDM_CONTROL			0x0140
 40#define PERIP_PWM_PDM_CONTROL_CH_MASK		0x1
 41#define PERIP_PWM_PDM_CONTROL_CH_SHIFT(ch)	((ch) * 4)
 42
 43#define IMG_PWM_PM_TIMEOUT			1000 /* ms */
 44
 45/*
 46 * PWM period is specified with a timebase register,
 47 * in number of step periods. The PWM duty cycle is also
 48 * specified in step periods, in the [0, $timebase] range.
 49 * In other words, the timebase imposes the duty cycle
 50 * resolution. Therefore, let's constraint the timebase to
 51 * a minimum value to allow a sane range of duty cycle values.
 52 * Imposing a minimum timebase, will impose a maximum PWM frequency.
 53 *
 54 * The value chosen is completely arbitrary.
 55 */
 56#define MIN_TMBASE_STEPS			16
 57
 58#define IMG_PWM_NPWM				4
 59
 60struct img_pwm_soc_data {
 61	u32 max_timebase;
 62};
 63
 64struct img_pwm_chip {
 65	struct device	*dev;
 66	struct pwm_chip	chip;
 67	struct clk	*pwm_clk;
 68	struct clk	*sys_clk;
 69	void __iomem	*base;
 70	struct regmap	*periph_regs;
 71	int		max_period_ns;
 72	int		min_period_ns;
 73	const struct img_pwm_soc_data   *data;
 74	u32		suspend_ctrl_cfg;
 75	u32		suspend_ch_cfg[IMG_PWM_NPWM];
 76};
 77
 78static inline struct img_pwm_chip *to_img_pwm_chip(struct pwm_chip *chip)
 79{
 80	return container_of(chip, struct img_pwm_chip, chip);
 81}
 82
 83static inline void img_pwm_writel(struct img_pwm_chip *chip,
 84				  u32 reg, u32 val)
 85{
 86	writel(val, chip->base + reg);
 87}
 88
 89static inline u32 img_pwm_readl(struct img_pwm_chip *chip,
 90					 u32 reg)
 91{
 92	return readl(chip->base + reg);
 93}
 94
 95static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 96			  int duty_ns, int period_ns)
 97{
 98	u32 val, div, duty, timebase;
 99	unsigned long mul, output_clk_hz, input_clk_hz;
100	struct img_pwm_chip *pwm_chip = to_img_pwm_chip(chip);
101	unsigned int max_timebase = pwm_chip->data->max_timebase;
102	int ret;
103
104	if (period_ns < pwm_chip->min_period_ns ||
105	    period_ns > pwm_chip->max_period_ns) {
106		dev_err(chip->dev, "configured period not in range\n");
107		return -ERANGE;
108	}
109
110	input_clk_hz = clk_get_rate(pwm_chip->pwm_clk);
111	output_clk_hz = DIV_ROUND_UP(NSEC_PER_SEC, period_ns);
112
113	mul = DIV_ROUND_UP(input_clk_hz, output_clk_hz);
114	if (mul <= max_timebase) {
115		div = PWM_CTRL_CFG_NO_SUB_DIV;
116		timebase = DIV_ROUND_UP(mul, 1);
117	} else if (mul <= max_timebase * 8) {
118		div = PWM_CTRL_CFG_SUB_DIV0;
119		timebase = DIV_ROUND_UP(mul, 8);
120	} else if (mul <= max_timebase * 64) {
121		div = PWM_CTRL_CFG_SUB_DIV1;
122		timebase = DIV_ROUND_UP(mul, 64);
123	} else if (mul <= max_timebase * 512) {
124		div = PWM_CTRL_CFG_SUB_DIV0_DIV1;
125		timebase = DIV_ROUND_UP(mul, 512);
126	} else if (mul > max_timebase * 512) {
127		dev_err(chip->dev,
128			"failed to configure timebase steps/divider value\n");
129		return -EINVAL;
130	}
131
132	duty = DIV_ROUND_UP(timebase * duty_ns, period_ns);
133
134	ret = pm_runtime_get_sync(chip->dev);
135	if (ret < 0)
136		return ret;
137
138	val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
139	val &= ~(PWM_CTRL_CFG_DIV_MASK << PWM_CTRL_CFG_DIV_SHIFT(pwm->hwpwm));
140	val |= (div & PWM_CTRL_CFG_DIV_MASK) <<
141		PWM_CTRL_CFG_DIV_SHIFT(pwm->hwpwm);
142	img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val);
143
144	val = (duty << PWM_CH_CFG_DUTY_SHIFT) |
145	      (timebase << PWM_CH_CFG_TMBASE_SHIFT);
146	img_pwm_writel(pwm_chip, PWM_CH_CFG(pwm->hwpwm), val);
147
148	pm_runtime_mark_last_busy(chip->dev);
149	pm_runtime_put_autosuspend(chip->dev);
150
151	return 0;
152}
153
154static int img_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
155{
156	u32 val;
157	struct img_pwm_chip *pwm_chip = to_img_pwm_chip(chip);
158	int ret;
159
160	ret = pm_runtime_get_sync(chip->dev);
161	if (ret < 0)
162		return ret;
163
164	val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
165	val |= BIT(pwm->hwpwm);
166	img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val);
167
168	regmap_update_bits(pwm_chip->periph_regs, PERIP_PWM_PDM_CONTROL,
169			   PERIP_PWM_PDM_CONTROL_CH_MASK <<
170			   PERIP_PWM_PDM_CONTROL_CH_SHIFT(pwm->hwpwm), 0);
171
172	return 0;
173}
174
175static void img_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
176{
177	u32 val;
178	struct img_pwm_chip *pwm_chip = to_img_pwm_chip(chip);
179
180	val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
181	val &= ~BIT(pwm->hwpwm);
182	img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val);
183
184	pm_runtime_mark_last_busy(chip->dev);
185	pm_runtime_put_autosuspend(chip->dev);
186}
187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188static const struct pwm_ops img_pwm_ops = {
189	.config = img_pwm_config,
190	.enable = img_pwm_enable,
191	.disable = img_pwm_disable,
192	.owner = THIS_MODULE,
193};
194
195static const struct img_pwm_soc_data pistachio_pwm = {
196	.max_timebase = 255,
197};
198
199static const struct of_device_id img_pwm_of_match[] = {
200	{
201		.compatible = "img,pistachio-pwm",
202		.data = &pistachio_pwm,
203	},
204	{ }
205};
206MODULE_DEVICE_TABLE(of, img_pwm_of_match);
207
208static int img_pwm_runtime_suspend(struct device *dev)
209{
210	struct img_pwm_chip *pwm_chip = dev_get_drvdata(dev);
211
212	clk_disable_unprepare(pwm_chip->pwm_clk);
213	clk_disable_unprepare(pwm_chip->sys_clk);
214
215	return 0;
216}
217
218static int img_pwm_runtime_resume(struct device *dev)
219{
220	struct img_pwm_chip *pwm_chip = dev_get_drvdata(dev);
221	int ret;
222
223	ret = clk_prepare_enable(pwm_chip->sys_clk);
224	if (ret < 0) {
225		dev_err(dev, "could not prepare or enable sys clock\n");
226		return ret;
227	}
228
229	ret = clk_prepare_enable(pwm_chip->pwm_clk);
230	if (ret < 0) {
231		dev_err(dev, "could not prepare or enable pwm clock\n");
232		clk_disable_unprepare(pwm_chip->sys_clk);
233		return ret;
234	}
235
236	return 0;
237}
238
239static int img_pwm_probe(struct platform_device *pdev)
240{
241	int ret;
242	u64 val;
243	unsigned long clk_rate;
244	struct resource *res;
245	struct img_pwm_chip *pwm;
246	const struct of_device_id *of_dev_id;
247
248	pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
249	if (!pwm)
250		return -ENOMEM;
251
252	pwm->dev = &pdev->dev;
 
 
 
 
 
 
253
254	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
255	pwm->base = devm_ioremap_resource(&pdev->dev, res);
256	if (IS_ERR(pwm->base))
257		return PTR_ERR(pwm->base);
258
259	of_dev_id = of_match_device(img_pwm_of_match, &pdev->dev);
260	if (!of_dev_id)
261		return -ENODEV;
262	pwm->data = of_dev_id->data;
263
264	pwm->periph_regs = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
265							   "img,cr-periph");
266	if (IS_ERR(pwm->periph_regs))
267		return PTR_ERR(pwm->periph_regs);
268
269	pwm->sys_clk = devm_clk_get(&pdev->dev, "sys");
270	if (IS_ERR(pwm->sys_clk)) {
271		dev_err(&pdev->dev, "failed to get system clock\n");
272		return PTR_ERR(pwm->sys_clk);
273	}
274
275	pwm->pwm_clk = devm_clk_get(&pdev->dev, "pwm");
276	if (IS_ERR(pwm->pwm_clk)) {
277		dev_err(&pdev->dev, "failed to get pwm clock\n");
278		return PTR_ERR(pwm->pwm_clk);
279	}
280
 
 
281	pm_runtime_set_autosuspend_delay(&pdev->dev, IMG_PWM_PM_TIMEOUT);
282	pm_runtime_use_autosuspend(&pdev->dev);
283	pm_runtime_enable(&pdev->dev);
284	if (!pm_runtime_enabled(&pdev->dev)) {
285		ret = img_pwm_runtime_resume(&pdev->dev);
286		if (ret)
287			goto err_pm_disable;
288	}
289
290	clk_rate = clk_get_rate(pwm->pwm_clk);
291	if (!clk_rate) {
292		dev_err(&pdev->dev, "pwm clock has no frequency\n");
293		ret = -EINVAL;
294		goto err_suspend;
295	}
296
297	/* The maximum input clock divider is 512 */
298	val = (u64)NSEC_PER_SEC * 512 * pwm->data->max_timebase;
299	do_div(val, clk_rate);
300	pwm->max_period_ns = val;
301
302	val = (u64)NSEC_PER_SEC * MIN_TMBASE_STEPS;
303	do_div(val, clk_rate);
304	pwm->min_period_ns = val;
305
306	pwm->chip.dev = &pdev->dev;
307	pwm->chip.ops = &img_pwm_ops;
308	pwm->chip.base = -1;
309	pwm->chip.npwm = IMG_PWM_NPWM;
310
311	ret = pwmchip_add(&pwm->chip);
312	if (ret < 0) {
313		dev_err(&pdev->dev, "pwmchip_add failed: %d\n", ret);
314		goto err_suspend;
315	}
316
317	platform_set_drvdata(pdev, pwm);
318	return 0;
319
320err_suspend:
321	if (!pm_runtime_enabled(&pdev->dev))
322		img_pwm_runtime_suspend(&pdev->dev);
323err_pm_disable:
324	pm_runtime_disable(&pdev->dev);
325	pm_runtime_dont_use_autosuspend(&pdev->dev);
326	return ret;
327}
328
329static int img_pwm_remove(struct platform_device *pdev)
330{
331	struct img_pwm_chip *pwm_chip = platform_get_drvdata(pdev);
332	u32 val;
333	unsigned int i;
334	int ret;
335
336	ret = pm_runtime_get_sync(&pdev->dev);
337	if (ret < 0)
338		return ret;
339
340	for (i = 0; i < pwm_chip->chip.npwm; i++) {
341		val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
342		val &= ~BIT(i);
343		img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val);
344	}
345
346	pm_runtime_put(&pdev->dev);
347	pm_runtime_disable(&pdev->dev);
348	if (!pm_runtime_status_suspended(&pdev->dev))
349		img_pwm_runtime_suspend(&pdev->dev);
350
351	return pwmchip_remove(&pwm_chip->chip);
352}
353
354#ifdef CONFIG_PM_SLEEP
355static int img_pwm_suspend(struct device *dev)
356{
357	struct img_pwm_chip *pwm_chip = dev_get_drvdata(dev);
358	int i, ret;
359
360	if (pm_runtime_status_suspended(dev)) {
361		ret = img_pwm_runtime_resume(dev);
362		if (ret)
363			return ret;
364	}
365
366	for (i = 0; i < pwm_chip->chip.npwm; i++)
367		pwm_chip->suspend_ch_cfg[i] = img_pwm_readl(pwm_chip,
368							    PWM_CH_CFG(i));
369
370	pwm_chip->suspend_ctrl_cfg = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
371
372	img_pwm_runtime_suspend(dev);
373
374	return 0;
375}
376
377static int img_pwm_resume(struct device *dev)
378{
379	struct img_pwm_chip *pwm_chip = dev_get_drvdata(dev);
380	int ret;
381	int i;
382
383	ret = img_pwm_runtime_resume(dev);
384	if (ret)
385		return ret;
386
387	for (i = 0; i < pwm_chip->chip.npwm; i++)
388		img_pwm_writel(pwm_chip, PWM_CH_CFG(i),
389			       pwm_chip->suspend_ch_cfg[i]);
390
391	img_pwm_writel(pwm_chip, PWM_CTRL_CFG, pwm_chip->suspend_ctrl_cfg);
392
393	for (i = 0; i < pwm_chip->chip.npwm; i++)
394		if (pwm_chip->suspend_ctrl_cfg & BIT(i))
395			regmap_update_bits(pwm_chip->periph_regs,
396					   PERIP_PWM_PDM_CONTROL,
397					   PERIP_PWM_PDM_CONTROL_CH_MASK <<
398					   PERIP_PWM_PDM_CONTROL_CH_SHIFT(i),
399					   0);
400
401	if (pm_runtime_status_suspended(dev))
402		img_pwm_runtime_suspend(dev);
403
404	return 0;
405}
406#endif /* CONFIG_PM */
407
408static const struct dev_pm_ops img_pwm_pm_ops = {
409	SET_RUNTIME_PM_OPS(img_pwm_runtime_suspend,
410			   img_pwm_runtime_resume,
411			   NULL)
412	SET_SYSTEM_SLEEP_PM_OPS(img_pwm_suspend, img_pwm_resume)
413};
414
415static struct platform_driver img_pwm_driver = {
416	.driver = {
417		.name = "img-pwm",
418		.pm = &img_pwm_pm_ops,
419		.of_match_table = img_pwm_of_match,
420	},
421	.probe = img_pwm_probe,
422	.remove = img_pwm_remove,
423};
424module_platform_driver(img_pwm_driver);
425
426MODULE_AUTHOR("Sai Masarapu <Sai.Masarapu@imgtec.com>");
427MODULE_DESCRIPTION("Imagination Technologies PWM DAC driver");
428MODULE_LICENSE("GPL v2");