blob: f39399273426c9b729787778423a61da9884c8df [file] [log] [blame]
Thierry Reding6173f8f2012-08-31 11:46:24 +02001/*
2 * Copyright (C) ST-Ericsson SA 2010
3 *
4 * Author: Arun R Murthy <arun.murthy@stericsson.com>
5 * License terms: GNU General Public License (GPL) version 2
6 */
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;
25};
26
27static int ab8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
28 int duty_ns, int period_ns)
29{
30 int ret = 0;
31 unsigned int higher_val, lower_val;
32 u8 reg;
33
34 /*
35 * get the first 8 bits that are be written to
36 * AB8500_PWM_OUT_CTRL1_REG[0:7]
37 */
38 lower_val = duty_ns & 0x00FF;
39 /*
40 * get bits [9:10] that are to be written to
41 * AB8500_PWM_OUT_CTRL2_REG[0:1]
42 */
43 higher_val = ((duty_ns & 0x0300) >> 8);
44
45 reg = AB8500_PWM_OUT_CTRL1_REG + ((chip->base - 1) * 2);
46
47 ret = abx500_set_register_interruptible(chip->dev, AB8500_MISC,
48 reg, (u8)lower_val);
49 if (ret < 0)
50 return ret;
51 ret = abx500_set_register_interruptible(chip->dev, AB8500_MISC,
52 (reg + 1), (u8)higher_val);
53
54 return ret;
55}
56
57static int ab8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
58{
59 int ret;
60
61 ret = abx500_mask_and_set_register_interruptible(chip->dev,
62 AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG,
Axel Lin54b02342014-04-09 21:19:54 +080063 1 << (chip->base - 1), 1 << (chip->base - 1));
Thierry Reding6173f8f2012-08-31 11:46:24 +020064 if (ret < 0)
Axel Lin622fc5d2013-03-26 22:34:50 +080065 dev_err(chip->dev, "%s: Failed to enable PWM, Error %d\n",
Thierry Reding6173f8f2012-08-31 11:46:24 +020066 pwm->label, ret);
67 return ret;
68}
69
70static void ab8500_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
71{
72 int ret;
73
74 ret = abx500_mask_and_set_register_interruptible(chip->dev,
75 AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG,
Axel Lin54b02342014-04-09 21:19:54 +080076 1 << (chip->base - 1), 0);
Thierry Reding6173f8f2012-08-31 11:46:24 +020077 if (ret < 0)
78 dev_err(chip->dev, "%s: Failed to disable PWM, Error %d\n",
79 pwm->label, ret);
Thierry Reding6173f8f2012-08-31 11:46:24 +020080}
81
82static const struct pwm_ops ab8500_pwm_ops = {
83 .config = ab8500_pwm_config,
84 .enable = ab8500_pwm_enable,
85 .disable = ab8500_pwm_disable,
Axel Linfa0abee2013-03-31 11:14:02 +080086 .owner = THIS_MODULE,
Thierry Reding6173f8f2012-08-31 11:46:24 +020087};
88
Bill Pemberton3e9fe832012-11-19 13:23:14 -050089static int ab8500_pwm_probe(struct platform_device *pdev)
Thierry Reding6173f8f2012-08-31 11:46:24 +020090{
91 struct ab8500_pwm_chip *ab8500;
92 int err;
93
94 /*
95 * Nothing to be done in probe, this is required to get the
96 * device which is required for ab8500 read and write
97 */
Jingoo Han482467a2013-03-08 12:45:58 +090098 ab8500 = devm_kzalloc(&pdev->dev, sizeof(*ab8500), GFP_KERNEL);
Jingoo Hana2fc1db2014-04-23 18:39:26 +090099 if (ab8500 == NULL)
Thierry Reding6173f8f2012-08-31 11:46:24 +0200100 return -ENOMEM;
Thierry Reding6173f8f2012-08-31 11:46:24 +0200101
102 ab8500->chip.dev = &pdev->dev;
103 ab8500->chip.ops = &ab8500_pwm_ops;
104 ab8500->chip.base = pdev->id;
105 ab8500->chip.npwm = 1;
106
107 err = pwmchip_add(&ab8500->chip);
Jingoo Han482467a2013-03-08 12:45:58 +0900108 if (err < 0)
Thierry Reding6173f8f2012-08-31 11:46:24 +0200109 return err;
Thierry Reding6173f8f2012-08-31 11:46:24 +0200110
111 dev_dbg(&pdev->dev, "pwm probe successful\n");
112 platform_set_drvdata(pdev, ab8500);
113
114 return 0;
115}
116
Bill Pemberton77f37912012-11-19 13:26:09 -0500117static int ab8500_pwm_remove(struct platform_device *pdev)
Thierry Reding6173f8f2012-08-31 11:46:24 +0200118{
119 struct ab8500_pwm_chip *ab8500 = platform_get_drvdata(pdev);
120 int err;
121
122 err = pwmchip_remove(&ab8500->chip);
123 if (err < 0)
124 return err;
125
126 dev_dbg(&pdev->dev, "pwm driver removed\n");
Thierry Reding6173f8f2012-08-31 11:46:24 +0200127
128 return 0;
129}
130
131static struct platform_driver ab8500_pwm_driver = {
132 .driver = {
133 .name = "ab8500-pwm",
Thierry Reding6173f8f2012-08-31 11:46:24 +0200134 },
135 .probe = ab8500_pwm_probe,
Bill Pembertonfd109112012-11-19 13:21:28 -0500136 .remove = ab8500_pwm_remove,
Thierry Reding6173f8f2012-08-31 11:46:24 +0200137};
138module_platform_driver(ab8500_pwm_driver);
139
140MODULE_AUTHOR("Arun MURTHY <arun.murthy@stericsson.com>");
141MODULE_DESCRIPTION("AB8500 Pulse Width Modulation Driver");
142MODULE_ALIAS("platform:ab8500-pwm");
143MODULE_LICENSE("GPL v2");