Arun Murthy | f0f05b1 | 2010-09-06 12:24:52 +0530 | [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/ab8500.h> |
| 12 | #include <linux/mfd/abx500.h> |
Paul Gortmaker | eb12a67 | 2011-07-03 15:14:56 -0400 | [diff] [blame] | 13 | #include <linux/module.h> |
Arun Murthy | f0f05b1 | 2010-09-06 12:24:52 +0530 | [diff] [blame] | 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 | |
| 23 | /* backlight driver constants */ |
| 24 | #define ENABLE_PWM 1 |
| 25 | #define DISABLE_PWM 0 |
| 26 | |
| 27 | struct pwm_device { |
| 28 | struct device *dev; |
| 29 | struct list_head node; |
| 30 | const char *label; |
| 31 | unsigned int pwm_id; |
| 32 | }; |
| 33 | |
| 34 | static LIST_HEAD(pwm_list); |
| 35 | |
| 36 | int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) |
| 37 | { |
| 38 | int ret = 0; |
| 39 | unsigned int higher_val, lower_val; |
| 40 | u8 reg; |
| 41 | |
| 42 | /* |
| 43 | * get the first 8 bits that are be written to |
| 44 | * AB8500_PWM_OUT_CTRL1_REG[0:7] |
| 45 | */ |
| 46 | lower_val = duty_ns & 0x00FF; |
| 47 | /* |
| 48 | * get bits [9:10] that are to be written to |
| 49 | * AB8500_PWM_OUT_CTRL2_REG[0:1] |
| 50 | */ |
| 51 | higher_val = ((duty_ns & 0x0300) >> 8); |
| 52 | |
| 53 | reg = AB8500_PWM_OUT_CTRL1_REG + ((pwm->pwm_id - 1) * 2); |
| 54 | |
| 55 | ret = abx500_set_register_interruptible(pwm->dev, AB8500_MISC, |
| 56 | reg, (u8)lower_val); |
| 57 | if (ret < 0) |
| 58 | return ret; |
| 59 | ret = abx500_set_register_interruptible(pwm->dev, AB8500_MISC, |
| 60 | (reg + 1), (u8)higher_val); |
| 61 | |
| 62 | return ret; |
| 63 | } |
| 64 | EXPORT_SYMBOL(pwm_config); |
| 65 | |
| 66 | int pwm_enable(struct pwm_device *pwm) |
| 67 | { |
| 68 | int ret; |
| 69 | |
| 70 | ret = abx500_mask_and_set_register_interruptible(pwm->dev, |
| 71 | AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG, |
| 72 | 1 << (pwm->pwm_id-1), ENABLE_PWM); |
| 73 | if (ret < 0) |
| 74 | dev_err(pwm->dev, "%s: Failed to disable PWM, Error %d\n", |
| 75 | pwm->label, ret); |
| 76 | return ret; |
| 77 | } |
| 78 | EXPORT_SYMBOL(pwm_enable); |
| 79 | |
| 80 | void pwm_disable(struct pwm_device *pwm) |
| 81 | { |
| 82 | int ret; |
| 83 | |
| 84 | ret = abx500_mask_and_set_register_interruptible(pwm->dev, |
| 85 | AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG, |
| 86 | 1 << (pwm->pwm_id-1), DISABLE_PWM); |
| 87 | if (ret < 0) |
| 88 | dev_err(pwm->dev, "%s: Failed to disable PWM, Error %d\n", |
| 89 | pwm->label, ret); |
| 90 | return; |
| 91 | } |
| 92 | EXPORT_SYMBOL(pwm_disable); |
| 93 | |
| 94 | struct pwm_device *pwm_request(int pwm_id, const char *label) |
| 95 | { |
| 96 | struct pwm_device *pwm; |
| 97 | |
| 98 | list_for_each_entry(pwm, &pwm_list, node) { |
| 99 | if (pwm->pwm_id == pwm_id) { |
| 100 | pwm->label = label; |
| 101 | pwm->pwm_id = pwm_id; |
| 102 | return pwm; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return ERR_PTR(-ENOENT); |
| 107 | } |
| 108 | EXPORT_SYMBOL(pwm_request); |
| 109 | |
| 110 | void pwm_free(struct pwm_device *pwm) |
| 111 | { |
| 112 | pwm_disable(pwm); |
| 113 | } |
| 114 | EXPORT_SYMBOL(pwm_free); |
| 115 | |
| 116 | static int __devinit ab8500_pwm_probe(struct platform_device *pdev) |
| 117 | { |
| 118 | struct pwm_device *pwm; |
| 119 | /* |
| 120 | * Nothing to be done in probe, this is required to get the |
| 121 | * device which is required for ab8500 read and write |
| 122 | */ |
| 123 | pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL); |
| 124 | if (pwm == NULL) { |
| 125 | dev_err(&pdev->dev, "failed to allocate memory\n"); |
| 126 | return -ENOMEM; |
| 127 | } |
| 128 | pwm->dev = &pdev->dev; |
| 129 | pwm->pwm_id = pdev->id; |
| 130 | list_add_tail(&pwm->node, &pwm_list); |
| 131 | platform_set_drvdata(pdev, pwm); |
| 132 | dev_dbg(pwm->dev, "pwm probe successful\n"); |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static int __devexit ab8500_pwm_remove(struct platform_device *pdev) |
| 137 | { |
| 138 | struct pwm_device *pwm = platform_get_drvdata(pdev); |
| 139 | list_del(&pwm->node); |
| 140 | dev_dbg(&pdev->dev, "pwm driver removed\n"); |
| 141 | kfree(pwm); |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static struct platform_driver ab8500_pwm_driver = { |
| 146 | .driver = { |
| 147 | .name = "ab8500-pwm", |
| 148 | .owner = THIS_MODULE, |
| 149 | }, |
| 150 | .probe = ab8500_pwm_probe, |
| 151 | .remove = __devexit_p(ab8500_pwm_remove), |
| 152 | }; |
| 153 | |
| 154 | static int __init ab8500_pwm_init(void) |
| 155 | { |
| 156 | return platform_driver_register(&ab8500_pwm_driver); |
| 157 | } |
| 158 | |
| 159 | static void __exit ab8500_pwm_exit(void) |
| 160 | { |
| 161 | platform_driver_unregister(&ab8500_pwm_driver); |
| 162 | } |
| 163 | |
| 164 | subsys_initcall(ab8500_pwm_init); |
| 165 | module_exit(ab8500_pwm_exit); |
| 166 | MODULE_AUTHOR("Arun MURTHY <arun.murthy@stericsson.com>"); |
| 167 | MODULE_DESCRIPTION("AB8500 Pulse Width Modulation Driver"); |
Axel Lin | 37b7bf6 | 2011-08-25 15:59:20 -0700 | [diff] [blame] | 168 | MODULE_ALIAS("platform:ab8500-pwm"); |
Arun Murthy | f0f05b1 | 2010-09-06 12:24:52 +0530 | [diff] [blame] | 169 | MODULE_LICENSE("GPL v2"); |