xref: /openbmc/linux/drivers/pwm/pwm-crc.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a3f37a10SShobhit Kumar /*
3a3f37a10SShobhit Kumar  * Copyright (C) 2015 Intel Corporation. All rights reserved.
4a3f37a10SShobhit Kumar  *
5a3f37a10SShobhit Kumar  * Author: Shobhit Kumar <shobhit.kumar@intel.com>
6a3f37a10SShobhit Kumar  */
7a3f37a10SShobhit Kumar 
8a3f37a10SShobhit Kumar #include <linux/platform_device.h>
9a3f37a10SShobhit Kumar #include <linux/regmap.h>
10a3f37a10SShobhit Kumar #include <linux/mfd/intel_soc_pmic.h>
11a3f37a10SShobhit Kumar #include <linux/pwm.h>
12a3f37a10SShobhit Kumar 
13a3f37a10SShobhit Kumar #define PWM0_CLK_DIV		0x4B
14a3f37a10SShobhit Kumar #define  PWM_OUTPUT_ENABLE	BIT(7)
15a3f37a10SShobhit Kumar #define  PWM_DIV_CLK_0		0x00 /* DIVIDECLK = BASECLK */
16a3f37a10SShobhit Kumar #define  PWM_DIV_CLK_100	0x63 /* DIVIDECLK = BASECLK/100 */
17a3f37a10SShobhit Kumar #define  PWM_DIV_CLK_128	0x7F /* DIVIDECLK = BASECLK/128 */
18a3f37a10SShobhit Kumar 
19a3f37a10SShobhit Kumar #define PWM0_DUTY_CYCLE		0x4E
20a3f37a10SShobhit Kumar #define BACKLIGHT_EN		0x51
21a3f37a10SShobhit Kumar 
22a3f37a10SShobhit Kumar #define PWM_MAX_LEVEL		0xFF
23a3f37a10SShobhit Kumar 
2479e08992SHans de Goede #define PWM_BASE_CLK_MHZ	6	/* 6 MHz */
25a05af71fSHans de Goede #define PWM_MAX_PERIOD_NS	5461334	/* 183 Hz */
26a3f37a10SShobhit Kumar 
27a3f37a10SShobhit Kumar /**
28a3f37a10SShobhit Kumar  * struct crystalcove_pwm - Crystal Cove PWM controller
29a3f37a10SShobhit Kumar  * @chip: the abstract pwm_chip structure.
30a3f37a10SShobhit Kumar  * @regmap: the regmap from the parent device.
31a3f37a10SShobhit Kumar  */
32a3f37a10SShobhit Kumar struct crystalcove_pwm {
33a3f37a10SShobhit Kumar 	struct pwm_chip chip;
34a3f37a10SShobhit Kumar 	struct regmap *regmap;
35a3f37a10SShobhit Kumar };
36a3f37a10SShobhit Kumar 
to_crc_pwm(struct pwm_chip * chip)37*92f2de28SUwe Kleine-König static inline struct crystalcove_pwm *to_crc_pwm(struct pwm_chip *chip)
38a3f37a10SShobhit Kumar {
39*92f2de28SUwe Kleine-König 	return container_of(chip, struct crystalcove_pwm, chip);
40a3f37a10SShobhit Kumar }
41a3f37a10SShobhit Kumar 
crc_pwm_calc_clk_div(int period_ns)42a05af71fSHans de Goede static int crc_pwm_calc_clk_div(int period_ns)
43a05af71fSHans de Goede {
44a05af71fSHans de Goede 	int clk_div;
45a05af71fSHans de Goede 
46a05af71fSHans de Goede 	clk_div = PWM_BASE_CLK_MHZ * period_ns / (256 * NSEC_PER_USEC);
47a05af71fSHans de Goede 	/* clk_div 1 - 128, maps to register values 0-127 */
48a05af71fSHans de Goede 	if (clk_div > 0)
49a05af71fSHans de Goede 		clk_div--;
50a05af71fSHans de Goede 
51a05af71fSHans de Goede 	return clk_div;
52a05af71fSHans de Goede }
53a05af71fSHans de Goede 
crc_pwm_apply(struct pwm_chip * chip,struct pwm_device * pwm,const struct pwm_state * state)549fccec82SHans de Goede static int crc_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
559fccec82SHans de Goede 			 const struct pwm_state *state)
56a3f37a10SShobhit Kumar {
579fccec82SHans de Goede 	struct crystalcove_pwm *crc_pwm = to_crc_pwm(chip);
58a3f37a10SShobhit Kumar 	struct device *dev = crc_pwm->chip.dev;
599fccec82SHans de Goede 	int err;
60a3f37a10SShobhit Kumar 
619fccec82SHans de Goede 	if (state->period > PWM_MAX_PERIOD_NS) {
62a3f37a10SShobhit Kumar 		dev_err(dev, "un-supported period_ns\n");
63a3f37a10SShobhit Kumar 		return -EINVAL;
64a3f37a10SShobhit Kumar 	}
65a3f37a10SShobhit Kumar 
669fccec82SHans de Goede 	if (state->polarity != PWM_POLARITY_NORMAL)
672b1c1a5dSThierry Reding 		return -EINVAL;
68a3f37a10SShobhit Kumar 
699fccec82SHans de Goede 	if (pwm_is_enabled(pwm) && !state->enabled) {
709fccec82SHans de Goede 		err = regmap_write(crc_pwm->regmap, BACKLIGHT_EN, 0);
719fccec82SHans de Goede 		if (err) {
729fccec82SHans de Goede 			dev_err(dev, "Error writing BACKLIGHT_EN %d\n", err);
739fccec82SHans de Goede 			return err;
749fccec82SHans de Goede 		}
75a3f37a10SShobhit Kumar 	}
76a3f37a10SShobhit Kumar 
779fccec82SHans de Goede 	if (pwm_get_duty_cycle(pwm) != state->duty_cycle ||
789fccec82SHans de Goede 	    pwm_get_period(pwm) != state->period) {
799fccec82SHans de Goede 		u64 level = state->duty_cycle * PWM_MAX_LEVEL;
809fccec82SHans de Goede 
819fccec82SHans de Goede 		do_div(level, state->period);
829fccec82SHans de Goede 
839fccec82SHans de Goede 		err = regmap_write(crc_pwm->regmap, PWM0_DUTY_CYCLE, level);
849fccec82SHans de Goede 		if (err) {
859fccec82SHans de Goede 			dev_err(dev, "Error writing PWM0_DUTY_CYCLE %d\n", err);
869fccec82SHans de Goede 			return err;
879fccec82SHans de Goede 		}
889fccec82SHans de Goede 	}
899fccec82SHans de Goede 
909fccec82SHans de Goede 	if (pwm_is_enabled(pwm) && state->enabled &&
919fccec82SHans de Goede 	    pwm_get_period(pwm) != state->period) {
929fccec82SHans de Goede 		/* changing the clk divisor, clear PWM_OUTPUT_ENABLE first */
939fccec82SHans de Goede 		err = regmap_write(crc_pwm->regmap, PWM0_CLK_DIV, 0);
949fccec82SHans de Goede 		if (err) {
959fccec82SHans de Goede 			dev_err(dev, "Error writing PWM0_CLK_DIV %d\n", err);
969fccec82SHans de Goede 			return err;
979fccec82SHans de Goede 		}
989fccec82SHans de Goede 	}
999fccec82SHans de Goede 
1009fccec82SHans de Goede 	if (pwm_get_period(pwm) != state->period ||
1019fccec82SHans de Goede 	    pwm_is_enabled(pwm) != state->enabled) {
1029fccec82SHans de Goede 		int clk_div = crc_pwm_calc_clk_div(state->period);
1039fccec82SHans de Goede 		int pwm_output_enable = state->enabled ? PWM_OUTPUT_ENABLE : 0;
1049fccec82SHans de Goede 
1059fccec82SHans de Goede 		err = regmap_write(crc_pwm->regmap, PWM0_CLK_DIV,
1069fccec82SHans de Goede 				   clk_div | pwm_output_enable);
1079fccec82SHans de Goede 		if (err) {
1089fccec82SHans de Goede 			dev_err(dev, "Error writing PWM0_CLK_DIV %d\n", err);
1099fccec82SHans de Goede 			return err;
1109fccec82SHans de Goede 		}
1119fccec82SHans de Goede 	}
1129fccec82SHans de Goede 
1139fccec82SHans de Goede 	if (!pwm_is_enabled(pwm) && state->enabled) {
1149fccec82SHans de Goede 		err = regmap_write(crc_pwm->regmap, BACKLIGHT_EN, 1);
1159fccec82SHans de Goede 		if (err) {
1169fccec82SHans de Goede 			dev_err(dev, "Error writing BACKLIGHT_EN %d\n", err);
1179fccec82SHans de Goede 			return err;
1189fccec82SHans de Goede 		}
1199fccec82SHans de Goede 	}
120a3f37a10SShobhit Kumar 
121a3f37a10SShobhit Kumar 	return 0;
122a3f37a10SShobhit Kumar }
123a3f37a10SShobhit Kumar 
crc_pwm_get_state(struct pwm_chip * chip,struct pwm_device * pwm,struct pwm_state * state)1246c452cffSUwe Kleine-König static int crc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
125c86b155dSHans de Goede 			     struct pwm_state *state)
126c86b155dSHans de Goede {
127c86b155dSHans de Goede 	struct crystalcove_pwm *crc_pwm = to_crc_pwm(chip);
128c86b155dSHans de Goede 	struct device *dev = crc_pwm->chip.dev;
129c86b155dSHans de Goede 	unsigned int clk_div, clk_div_reg, duty_cycle_reg;
130c86b155dSHans de Goede 	int error;
131c86b155dSHans de Goede 
132c86b155dSHans de Goede 	error = regmap_read(crc_pwm->regmap, PWM0_CLK_DIV, &clk_div_reg);
133c86b155dSHans de Goede 	if (error) {
134c86b155dSHans de Goede 		dev_err(dev, "Error reading PWM0_CLK_DIV %d\n", error);
1359c9d5e99SUwe Kleine-König 		return error;
136c86b155dSHans de Goede 	}
137c86b155dSHans de Goede 
138c86b155dSHans de Goede 	error = regmap_read(crc_pwm->regmap, PWM0_DUTY_CYCLE, &duty_cycle_reg);
139c86b155dSHans de Goede 	if (error) {
140c86b155dSHans de Goede 		dev_err(dev, "Error reading PWM0_DUTY_CYCLE %d\n", error);
1419c9d5e99SUwe Kleine-König 		return error;
142c86b155dSHans de Goede 	}
143c86b155dSHans de Goede 
144c86b155dSHans de Goede 	clk_div = (clk_div_reg & ~PWM_OUTPUT_ENABLE) + 1;
145c86b155dSHans de Goede 
146c86b155dSHans de Goede 	state->period =
147c86b155dSHans de Goede 		DIV_ROUND_UP(clk_div * NSEC_PER_USEC * 256, PWM_BASE_CLK_MHZ);
148c86b155dSHans de Goede 	state->duty_cycle =
149c86b155dSHans de Goede 		DIV_ROUND_UP_ULL(duty_cycle_reg * state->period, PWM_MAX_LEVEL);
150c86b155dSHans de Goede 	state->polarity = PWM_POLARITY_NORMAL;
151c86b155dSHans de Goede 	state->enabled = !!(clk_div_reg & PWM_OUTPUT_ENABLE);
1526c452cffSUwe Kleine-König 
1536c452cffSUwe Kleine-König 	return 0;
154c86b155dSHans de Goede }
155c86b155dSHans de Goede 
156a3f37a10SShobhit Kumar static const struct pwm_ops crc_pwm_ops = {
1579fccec82SHans de Goede 	.apply = crc_pwm_apply,
158c86b155dSHans de Goede 	.get_state = crc_pwm_get_state,
159a3f37a10SShobhit Kumar };
160a3f37a10SShobhit Kumar 
crystalcove_pwm_probe(struct platform_device * pdev)161a3f37a10SShobhit Kumar static int crystalcove_pwm_probe(struct platform_device *pdev)
162a3f37a10SShobhit Kumar {
163a3f37a10SShobhit Kumar 	struct crystalcove_pwm *pwm;
164a3f37a10SShobhit Kumar 	struct device *dev = pdev->dev.parent;
165a3f37a10SShobhit Kumar 	struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
166a3f37a10SShobhit Kumar 
167a3f37a10SShobhit Kumar 	pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
168a3f37a10SShobhit Kumar 	if (!pwm)
169a3f37a10SShobhit Kumar 		return -ENOMEM;
170a3f37a10SShobhit Kumar 
171a3f37a10SShobhit Kumar 	pwm->chip.dev = &pdev->dev;
172a3f37a10SShobhit Kumar 	pwm->chip.ops = &crc_pwm_ops;
173a3f37a10SShobhit Kumar 	pwm->chip.npwm = 1;
174a3f37a10SShobhit Kumar 
175a3f37a10SShobhit Kumar 	/* get the PMIC regmap */
176a3f37a10SShobhit Kumar 	pwm->regmap = pmic->regmap;
177a3f37a10SShobhit Kumar 
17866a03c4fSUwe Kleine-König 	return devm_pwmchip_add(&pdev->dev, &pwm->chip);
179a3f37a10SShobhit Kumar }
180a3f37a10SShobhit Kumar 
181a3f37a10SShobhit Kumar static struct platform_driver crystalcove_pwm_driver = {
182a3f37a10SShobhit Kumar 	.probe = crystalcove_pwm_probe,
183a3f37a10SShobhit Kumar 	.driver = {
184a3f37a10SShobhit Kumar 		.name = "crystal_cove_pwm",
185a3f37a10SShobhit Kumar 	},
186a3f37a10SShobhit Kumar };
187a3f37a10SShobhit Kumar 
188a3f37a10SShobhit Kumar builtin_platform_driver(crystalcove_pwm_driver);
189