1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson SA 2010
5 * Author: Arun R Murthy <arun.murthy@stericsson.com>
8 #include <linux/platform_device.h>
9 #include <linux/slab.h>
10 #include <linux/pwm.h>
11 #include <linux/mfd/abx500.h>
12 #include <linux/mfd/abx500/ab8500.h>
13 #include <linux/module.h>
19 #define AB8500_PWM_OUT_CTRL1_REG 0x60
20 #define AB8500_PWM_OUT_CTRL2_REG 0x61
21 #define AB8500_PWM_OUT_CTRL7_REG 0x66
23 struct ab8500_pwm_chip
{
27 static int ab8500_pwm_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
28 int duty_ns
, int period_ns
)
31 unsigned int higher_val
, lower_val
;
35 * get the first 8 bits that are be written to
36 * AB8500_PWM_OUT_CTRL1_REG[0:7]
38 lower_val
= duty_ns
& 0x00FF;
40 * get bits [9:10] that are to be written to
41 * AB8500_PWM_OUT_CTRL2_REG[0:1]
43 higher_val
= ((duty_ns
& 0x0300) >> 8);
45 reg
= AB8500_PWM_OUT_CTRL1_REG
+ ((chip
->base
- 1) * 2);
47 ret
= abx500_set_register_interruptible(chip
->dev
, AB8500_MISC
,
51 ret
= abx500_set_register_interruptible(chip
->dev
, AB8500_MISC
,
52 (reg
+ 1), (u8
)higher_val
);
57 static int ab8500_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
61 ret
= abx500_mask_and_set_register_interruptible(chip
->dev
,
62 AB8500_MISC
, AB8500_PWM_OUT_CTRL7_REG
,
63 1 << (chip
->base
- 1), 1 << (chip
->base
- 1));
65 dev_err(chip
->dev
, "%s: Failed to enable PWM, Error %d\n",
70 static void ab8500_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
74 ret
= abx500_mask_and_set_register_interruptible(chip
->dev
,
75 AB8500_MISC
, AB8500_PWM_OUT_CTRL7_REG
,
76 1 << (chip
->base
- 1), 0);
78 dev_err(chip
->dev
, "%s: Failed to disable PWM, Error %d\n",
82 static const struct pwm_ops ab8500_pwm_ops
= {
83 .config
= ab8500_pwm_config
,
84 .enable
= ab8500_pwm_enable
,
85 .disable
= ab8500_pwm_disable
,
89 static int ab8500_pwm_probe(struct platform_device
*pdev
)
91 struct ab8500_pwm_chip
*ab8500
;
95 * Nothing to be done in probe, this is required to get the
96 * device which is required for ab8500 read and write
98 ab8500
= devm_kzalloc(&pdev
->dev
, sizeof(*ab8500
), GFP_KERNEL
);
102 ab8500
->chip
.dev
= &pdev
->dev
;
103 ab8500
->chip
.ops
= &ab8500_pwm_ops
;
104 ab8500
->chip
.base
= -1;
105 ab8500
->chip
.npwm
= 1;
107 err
= pwmchip_add(&ab8500
->chip
);
109 return dev_err_probe(&pdev
->dev
, err
, "Failed to add pwm chip\n");
111 dev_dbg(&pdev
->dev
, "pwm probe successful\n");
112 platform_set_drvdata(pdev
, ab8500
);
117 static int ab8500_pwm_remove(struct platform_device
*pdev
)
119 struct ab8500_pwm_chip
*ab8500
= platform_get_drvdata(pdev
);
122 err
= pwmchip_remove(&ab8500
->chip
);
126 dev_dbg(&pdev
->dev
, "pwm driver removed\n");
131 static struct platform_driver ab8500_pwm_driver
= {
133 .name
= "ab8500-pwm",
135 .probe
= ab8500_pwm_probe
,
136 .remove
= ab8500_pwm_remove
,
138 module_platform_driver(ab8500_pwm_driver
);
140 MODULE_AUTHOR("Arun MURTHY <arun.murthy@stericsson.com>");
141 MODULE_DESCRIPTION("AB8500 Pulse Width Modulation Driver");
142 MODULE_ALIAS("platform:ab8500-pwm");
143 MODULE_LICENSE("GPL v2");