blob: ad37bc46f2721424481e386da0af1a1ab6f25afe [file] [log] [blame]
Thomas Gleixneraf873fc2019-05-28 09:57:21 -07001// SPDX-License-Identifier: GPL-2.0-only
Thierry Reding6173f8f2012-08-31 11:46:24 +02002/*
3 * Copyright (C) ST-Ericsson SA 2010
4 *
5 * Author: Arun R Murthy <arun.murthy@stericsson.com>
Thierry Reding6173f8f2012-08-31 11:46:24 +02006 */
7#include <linux/err.h>
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>
14
15/*
16 * PWM Out generators
17 * Bank: 0x10
18 */
19#define AB8500_PWM_OUT_CTRL1_REG 0x60
20#define AB8500_PWM_OUT_CTRL2_REG 0x61
21#define AB8500_PWM_OUT_CTRL7_REG 0x66
22
Thierry Reding6173f8f2012-08-31 11:46:24 +020023struct ab8500_pwm_chip {
24 struct pwm_chip chip;
Uwe Kleine-Königeb41f332021-07-05 18:55:10 +020025 unsigned int hwid;
Thierry Reding6173f8f2012-08-31 11:46:24 +020026};
27
Uwe Kleine-Königeb41f332021-07-05 18:55:10 +020028static struct ab8500_pwm_chip *ab8500_pwm_from_chip(struct pwm_chip *chip)
29{
30 return container_of(chip, struct ab8500_pwm_chip, chip);
31}
32
Uwe Kleine-Königacf34022021-03-01 19:45:37 +010033static int ab8500_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
34 const struct pwm_state *state)
Thierry Reding6173f8f2012-08-31 11:46:24 +020035{
Uwe Kleine-Königacf34022021-03-01 19:45:37 +010036 int ret;
Thierry Reding6173f8f2012-08-31 11:46:24 +020037 u8 reg;
Uwe Kleine-Königacf34022021-03-01 19:45:37 +010038 unsigned int higher_val, lower_val;
Uwe Kleine-Königeb41f332021-07-05 18:55:10 +020039 struct ab8500_pwm_chip *ab8500 = ab8500_pwm_from_chip(chip);
Uwe Kleine-Königacf34022021-03-01 19:45:37 +010040
41 if (state->polarity != PWM_POLARITY_NORMAL)
42 return -EINVAL;
43
44 if (!state->enabled) {
45 ret = abx500_mask_and_set_register_interruptible(chip->dev,
46 AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG,
Uwe Kleine-Königeb41f332021-07-05 18:55:10 +020047 1 << ab8500->hwid, 0);
Uwe Kleine-Königacf34022021-03-01 19:45:37 +010048
49 if (ret < 0)
50 dev_err(chip->dev, "%s: Failed to disable PWM, Error %d\n",
51 pwm->label, ret);
52 return ret;
53 }
Thierry Reding6173f8f2012-08-31 11:46:24 +020054
55 /*
56 * get the first 8 bits that are be written to
57 * AB8500_PWM_OUT_CTRL1_REG[0:7]
58 */
Uwe Kleine-Königacf34022021-03-01 19:45:37 +010059 lower_val = state->duty_cycle & 0x00FF;
Thierry Reding6173f8f2012-08-31 11:46:24 +020060 /*
61 * get bits [9:10] that are to be written to
62 * AB8500_PWM_OUT_CTRL2_REG[0:1]
63 */
Uwe Kleine-Königacf34022021-03-01 19:45:37 +010064 higher_val = ((state->duty_cycle & 0x0300) >> 8);
Thierry Reding6173f8f2012-08-31 11:46:24 +020065
Uwe Kleine-Königeb41f332021-07-05 18:55:10 +020066 reg = AB8500_PWM_OUT_CTRL1_REG + (ab8500->hwid * 2);
Thierry Reding6173f8f2012-08-31 11:46:24 +020067
68 ret = abx500_set_register_interruptible(chip->dev, AB8500_MISC,
69 reg, (u8)lower_val);
70 if (ret < 0)
71 return ret;
Uwe Kleine-Königacf34022021-03-01 19:45:37 +010072
Thierry Reding6173f8f2012-08-31 11:46:24 +020073 ret = abx500_set_register_interruptible(chip->dev, AB8500_MISC,
74 (reg + 1), (u8)higher_val);
Uwe Kleine-Königacf34022021-03-01 19:45:37 +010075 if (ret < 0)
76 return ret;
Thierry Reding6173f8f2012-08-31 11:46:24 +020077
78 ret = abx500_mask_and_set_register_interruptible(chip->dev,
79 AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG,
Uwe Kleine-Königeb41f332021-07-05 18:55:10 +020080 1 << ab8500->hwid, 1 << ab8500->hwid);
Thierry Reding6173f8f2012-08-31 11:46:24 +020081 if (ret < 0)
Axel Lin622fc5d2013-03-26 22:34:50 +080082 dev_err(chip->dev, "%s: Failed to enable PWM, Error %d\n",
Thierry Reding6173f8f2012-08-31 11:46:24 +020083 pwm->label, ret);
Uwe Kleine-Königacf34022021-03-01 19:45:37 +010084
Thierry Reding6173f8f2012-08-31 11:46:24 +020085 return ret;
86}
87
Thierry Reding6173f8f2012-08-31 11:46:24 +020088static const struct pwm_ops ab8500_pwm_ops = {
Uwe Kleine-Königacf34022021-03-01 19:45:37 +010089 .apply = ab8500_pwm_apply,
Axel Linfa0abee2013-03-31 11:14:02 +080090 .owner = THIS_MODULE,
Thierry Reding6173f8f2012-08-31 11:46:24 +020091};
92
Bill Pemberton3e9fe832012-11-19 13:23:14 -050093static int ab8500_pwm_probe(struct platform_device *pdev)
Thierry Reding6173f8f2012-08-31 11:46:24 +020094{
95 struct ab8500_pwm_chip *ab8500;
96 int err;
97
Uwe Kleine-Königeb41f332021-07-05 18:55:10 +020098 if (pdev->id < 1 || pdev->id > 31)
99 return dev_err_probe(&pdev->dev, EINVAL, "Invalid device id %d\n", pdev->id);
100
Thierry Reding6173f8f2012-08-31 11:46:24 +0200101 /*
102 * Nothing to be done in probe, this is required to get the
103 * device which is required for ab8500 read and write
104 */
Jingoo Han482467a2013-03-08 12:45:58 +0900105 ab8500 = devm_kzalloc(&pdev->dev, sizeof(*ab8500), GFP_KERNEL);
Jingoo Hana2fc1db2014-04-23 18:39:26 +0900106 if (ab8500 == NULL)
Thierry Reding6173f8f2012-08-31 11:46:24 +0200107 return -ENOMEM;
Thierry Reding6173f8f2012-08-31 11:46:24 +0200108
109 ab8500->chip.dev = &pdev->dev;
110 ab8500->chip.ops = &ab8500_pwm_ops;
Thierry Reding6173f8f2012-08-31 11:46:24 +0200111 ab8500->chip.npwm = 1;
Uwe Kleine-Königeb41f332021-07-05 18:55:10 +0200112 ab8500->hwid = pdev->id - 1;
Thierry Reding6173f8f2012-08-31 11:46:24 +0200113
Uwe Kleine-König14ac9e12021-07-07 18:28:00 +0200114 err = devm_pwmchip_add(&pdev->dev, &ab8500->chip);
Jingoo Han482467a2013-03-08 12:45:58 +0900115 if (err < 0)
Uwe Kleine-König2e978a42020-08-12 09:52:14 +0200116 return dev_err_probe(&pdev->dev, err, "Failed to add pwm chip\n");
Thierry Reding6173f8f2012-08-31 11:46:24 +0200117
118 dev_dbg(&pdev->dev, "pwm probe successful\n");
Thierry Reding6173f8f2012-08-31 11:46:24 +0200119
120 return 0;
121}
122
123static struct platform_driver ab8500_pwm_driver = {
124 .driver = {
125 .name = "ab8500-pwm",
Thierry Reding6173f8f2012-08-31 11:46:24 +0200126 },
127 .probe = ab8500_pwm_probe,
Thierry Reding6173f8f2012-08-31 11:46:24 +0200128};
129module_platform_driver(ab8500_pwm_driver);
130
131MODULE_AUTHOR("Arun MURTHY <arun.murthy@stericsson.com>");
132MODULE_DESCRIPTION("AB8500 Pulse Width Modulation Driver");
133MODULE_ALIAS("platform:ab8500-pwm");
134MODULE_LICENSE("GPL v2");