Thierry Reding | 6173f8f | 2012-08-31 11:46:24 +0200 | [diff] [blame] | 1 | /* |
| 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 Reding | 6173f8f | 2012-08-31 11:46:24 +0200 | [diff] [blame] | 23 | struct ab8500_pwm_chip { |
| 24 | struct pwm_chip chip; |
| 25 | }; |
| 26 | |
| 27 | static 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 | |
| 57 | static 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 Lin | 54b0234 | 2014-04-09 21:19:54 +0800 | [diff] [blame] | 63 | 1 << (chip->base - 1), 1 << (chip->base - 1)); |
Thierry Reding | 6173f8f | 2012-08-31 11:46:24 +0200 | [diff] [blame] | 64 | if (ret < 0) |
Axel Lin | 622fc5d | 2013-03-26 22:34:50 +0800 | [diff] [blame] | 65 | dev_err(chip->dev, "%s: Failed to enable PWM, Error %d\n", |
Thierry Reding | 6173f8f | 2012-08-31 11:46:24 +0200 | [diff] [blame] | 66 | pwm->label, ret); |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | static 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 Lin | 54b0234 | 2014-04-09 21:19:54 +0800 | [diff] [blame] | 76 | 1 << (chip->base - 1), 0); |
Thierry Reding | 6173f8f | 2012-08-31 11:46:24 +0200 | [diff] [blame] | 77 | if (ret < 0) |
| 78 | dev_err(chip->dev, "%s: Failed to disable PWM, Error %d\n", |
| 79 | pwm->label, ret); |
Thierry Reding | 6173f8f | 2012-08-31 11:46:24 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static const struct pwm_ops ab8500_pwm_ops = { |
| 83 | .config = ab8500_pwm_config, |
| 84 | .enable = ab8500_pwm_enable, |
| 85 | .disable = ab8500_pwm_disable, |
Axel Lin | fa0abee | 2013-03-31 11:14:02 +0800 | [diff] [blame] | 86 | .owner = THIS_MODULE, |
Thierry Reding | 6173f8f | 2012-08-31 11:46:24 +0200 | [diff] [blame] | 87 | }; |
| 88 | |
Bill Pemberton | 3e9fe83 | 2012-11-19 13:23:14 -0500 | [diff] [blame] | 89 | static int ab8500_pwm_probe(struct platform_device *pdev) |
Thierry Reding | 6173f8f | 2012-08-31 11:46:24 +0200 | [diff] [blame] | 90 | { |
| 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 Han | 482467a | 2013-03-08 12:45:58 +0900 | [diff] [blame] | 98 | ab8500 = devm_kzalloc(&pdev->dev, sizeof(*ab8500), GFP_KERNEL); |
Jingoo Han | a2fc1db | 2014-04-23 18:39:26 +0900 | [diff] [blame] | 99 | if (ab8500 == NULL) |
Thierry Reding | 6173f8f | 2012-08-31 11:46:24 +0200 | [diff] [blame] | 100 | return -ENOMEM; |
Thierry Reding | 6173f8f | 2012-08-31 11:46:24 +0200 | [diff] [blame] | 101 | |
| 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 Han | 482467a | 2013-03-08 12:45:58 +0900 | [diff] [blame] | 108 | if (err < 0) |
Thierry Reding | 6173f8f | 2012-08-31 11:46:24 +0200 | [diff] [blame] | 109 | return err; |
Thierry Reding | 6173f8f | 2012-08-31 11:46:24 +0200 | [diff] [blame] | 110 | |
| 111 | dev_dbg(&pdev->dev, "pwm probe successful\n"); |
| 112 | platform_set_drvdata(pdev, ab8500); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
Bill Pemberton | 77f3791 | 2012-11-19 13:26:09 -0500 | [diff] [blame] | 117 | static int ab8500_pwm_remove(struct platform_device *pdev) |
Thierry Reding | 6173f8f | 2012-08-31 11:46:24 +0200 | [diff] [blame] | 118 | { |
| 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 Reding | 6173f8f | 2012-08-31 11:46:24 +0200 | [diff] [blame] | 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static struct platform_driver ab8500_pwm_driver = { |
| 132 | .driver = { |
| 133 | .name = "ab8500-pwm", |
Thierry Reding | 6173f8f | 2012-08-31 11:46:24 +0200 | [diff] [blame] | 134 | }, |
| 135 | .probe = ab8500_pwm_probe, |
Bill Pemberton | fd10911 | 2012-11-19 13:21:28 -0500 | [diff] [blame] | 136 | .remove = ab8500_pwm_remove, |
Thierry Reding | 6173f8f | 2012-08-31 11:46:24 +0200 | [diff] [blame] | 137 | }; |
| 138 | module_platform_driver(ab8500_pwm_driver); |
| 139 | |
| 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"); |