Willie Ruan | 70f989d | 2012-01-10 10:09:36 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | /* |
| 13 | * Qualcomm PM8XXX Pulse Width Modulation (PWM) driver |
| 14 | * |
| 15 | * The HW module is also called LPG (Light Pulse Generator). |
| 16 | */ |
| 17 | |
| 18 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 19 | |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/err.h> |
| 24 | #include <linux/debugfs.h> |
| 25 | #include <linux/mfd/pm8xxx/core.h> |
| 26 | #include <linux/mfd/pm8xxx/pwm.h> |
| 27 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 28 | #define PM8XXX_PWM_CHANNELS 3 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 29 | |
Jay Chokshi | 0c220cd | 2011-12-09 17:18:20 -0800 | [diff] [blame] | 30 | /* |
| 31 | * For the lack of better term to distinguish functional |
| 32 | * differences, hereby, LPG version 0 (V0, v0) denotes |
| 33 | * PM8058/8921, and version 1 (V1, v1) denotes |
| 34 | * PM8922/8038. |
| 35 | */ |
| 36 | #define PM8XXX_LPG_V0_PWM_CHANNELS 8 |
| 37 | #define PM8XXX_LPG_V1_PWM_CHANNELS 6 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 38 | #define PM8XXX_LPG_CTL_REGS 7 |
| 39 | |
| 40 | /* PM8XXX PWM */ |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 41 | #define SSBI_REG_ADDR_PWM1_CTRL1 0x88 |
| 42 | #define SSBI_REG_ADDR_PWM1_CTRL2 0x89 |
| 43 | #define SSBI_REG_ADDR_PWM_CTL(id, base) (id == 0 ? base : (base + (id << 1))) |
| 44 | #define SSBI_REG_ADDR_PWM_CTL1(id) SSBI_REG_ADDR_PWM_CTL(id, \ |
| 45 | SSBI_REG_ADDR_PWM1_CTRL1) |
| 46 | #define SSBI_REG_ADDR_PWM_CTL2(id) SSBI_REG_ADDR_PWM_CTL(id, \ |
| 47 | SSBI_REG_ADDR_PWM1_CTRL2) |
| 48 | |
| 49 | #define PM8XXX_PWM_CLK_SEL_SHIFT 6 |
| 50 | #define PM8XXX_PWM_CLK_SEL_MASK 0xC0 |
| 51 | #define PM8XXX_PWM_PREDIVIDE_SHIFT 5 |
| 52 | #define PM8XXX_PWM_PREDIVIDE_MASK 0x20 |
| 53 | #define PM8XXX_PWM_M_SHIFT 2 |
| 54 | #define PM8XXX_PWM_M_MASK 0x1C |
| 55 | #define PM8XXX_PWM_SIZE_SHIFT 1 |
| 56 | #define PM8XXX_PWM_SIZE_MASK 0x02 |
| 57 | #define PM8XXX_PWM_VALUE_BIT0 0x01 |
| 58 | #define PM8XXX_PWM_DISABLE 0x3F |
| 59 | |
| 60 | /* PM8XXX LPG PWM */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 61 | #define SSBI_REG_ADDR_LPG_CTL_BASE 0x13C |
| 62 | #define SSBI_REG_ADDR_LPG_CTL(n) (SSBI_REG_ADDR_LPG_CTL_BASE + (n)) |
| 63 | #define SSBI_REG_ADDR_LPG_BANK_SEL 0x143 |
| 64 | #define SSBI_REG_ADDR_LPG_BANK_EN 0x144 |
| 65 | #define SSBI_REG_ADDR_LPG_LUT_CFG0 0x145 |
| 66 | #define SSBI_REG_ADDR_LPG_LUT_CFG1 0x146 |
Jay Chokshi | b0a0fa5 | 2012-02-23 16:18:44 -0800 | [diff] [blame^] | 67 | #define SSBI_REG_ADDR_LPG_TEST 0x147 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 68 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 69 | /* LPG Control 0 */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 70 | #define PM8XXX_PWM_1KHZ_COUNT_MASK 0xF0 |
| 71 | #define PM8XXX_PWM_1KHZ_COUNT_SHIFT 4 |
| 72 | |
| 73 | #define PM8XXX_PWM_1KHZ_COUNT_MAX 15 |
| 74 | |
| 75 | #define PM8XXX_PWM_OUTPUT_EN 0x08 |
| 76 | #define PM8XXX_PWM_PWM_EN 0x04 |
| 77 | #define PM8XXX_PWM_RAMP_GEN_EN 0x02 |
| 78 | #define PM8XXX_PWM_RAMP_START 0x01 |
| 79 | |
| 80 | #define PM8XXX_PWM_PWM_START (PM8XXX_PWM_OUTPUT_EN \ |
| 81 | | PM8XXX_PWM_PWM_EN) |
| 82 | #define PM8XXX_PWM_RAMP_GEN_START (PM8XXX_PWM_RAMP_GEN_EN \ |
| 83 | | PM8XXX_PWM_RAMP_START) |
| 84 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 85 | /* LPG Control 1 */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 86 | #define PM8XXX_PWM_REVERSE_EN 0x80 |
| 87 | #define PM8XXX_PWM_BYPASS_LUT 0x40 |
| 88 | #define PM8XXX_PWM_HIGH_INDEX_MASK 0x3F |
| 89 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 90 | /* LPG Control 2 */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 91 | #define PM8XXX_PWM_LOOP_EN 0x80 |
| 92 | #define PM8XXX_PWM_RAMP_UP 0x40 |
| 93 | #define PM8XXX_PWM_LOW_INDEX_MASK 0x3F |
| 94 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 95 | /* LPG Control 3 */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 96 | #define PM8XXX_PWM_VALUE_BIT7_0 0xFF |
| 97 | #define PM8XXX_PWM_VALUE_BIT5_0 0x3F |
| 98 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 99 | /* LPG Control 4 */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 100 | #define PM8XXX_PWM_VALUE_BIT8 0x80 |
| 101 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 102 | #define PM8XXX_LPG_PWM_CLK_SEL_MASK 0x60 |
| 103 | #define PM8XXX_LPG_PWM_CLK_SEL_SHIFT 5 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 104 | |
| 105 | #define PM8XXX_PWM_CLK_SEL_NO 0 |
| 106 | #define PM8XXX_PWM_CLK_SEL_1KHZ 1 |
| 107 | #define PM8XXX_PWM_CLK_SEL_32KHZ 2 |
| 108 | #define PM8XXX_PWM_CLK_SEL_19P2MHZ 3 |
| 109 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 110 | #define PM8XXX_LPG_PWM_PREDIVIDE_MASK 0x18 |
| 111 | #define PM8XXX_LPG_PWM_PREDIVIDE_SHIFT 3 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 112 | |
| 113 | #define PM8XXX_PWM_PREDIVIDE_2 0 |
| 114 | #define PM8XXX_PWM_PREDIVIDE_3 1 |
| 115 | #define PM8XXX_PWM_PREDIVIDE_5 2 |
| 116 | #define PM8XXX_PWM_PREDIVIDE_6 3 |
| 117 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 118 | #define PM8XXX_LPG_PWM_M_MASK 0x07 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 119 | #define PM8XXX_PWM_M_MIN 0 |
| 120 | #define PM8XXX_PWM_M_MAX 7 |
| 121 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 122 | /* LPG Control 5 */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 123 | #define PM8XXX_PWM_PAUSE_COUNT_HI_MASK 0xFC |
| 124 | #define PM8XXX_PWM_PAUSE_COUNT_HI_SHIFT 2 |
| 125 | |
| 126 | #define PM8XXX_PWM_PAUSE_ENABLE_HIGH 0x02 |
| 127 | #define PM8XXX_PWM_SIZE_9_BIT 0x01 |
| 128 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 129 | /* LPG Control 6 */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 130 | #define PM8XXX_PWM_PAUSE_COUNT_LO_MASK 0xFC |
| 131 | #define PM8XXX_PWM_PAUSE_COUNT_LO_SHIFT 2 |
| 132 | |
| 133 | #define PM8XXX_PWM_PAUSE_ENABLE_LOW 0x02 |
| 134 | #define PM8XXX_PWM_RESERVED 0x01 |
| 135 | |
| 136 | #define PM8XXX_PWM_PAUSE_COUNT_MAX 56 /* < 2^6 = 64 */ |
| 137 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 138 | /* LPG LUT_CFG1 */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 139 | #define PM8XXX_PWM_LUT_READ 0x40 |
| 140 | |
Jay Chokshi | b0a0fa5 | 2012-02-23 16:18:44 -0800 | [diff] [blame^] | 141 | /* TEST */ |
| 142 | #define PM8XXX_PWM_DTEST_MASK 0x38 |
| 143 | #define PM8XXX_PWM_DTEST_SHIFT 3 |
| 144 | #define PM8XXX_PWM_DTEST_BANK_MASK 0x07 |
Jay Chokshi | 4acbdd5 | 2011-09-16 17:09:44 -0700 | [diff] [blame] | 145 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 146 | /* |
| 147 | * PWM Frequency = Clock Frequency / (N * T) |
| 148 | * or |
| 149 | * PWM Period = Clock Period * (N * T) |
| 150 | * where |
| 151 | * N = 2^9 or 2^6 for 9-bit or 6-bit PWM size |
| 152 | * T = Pre-divide * 2^m, where m = 0..7 (exponent) |
| 153 | * |
| 154 | * This is the formula to figure out m for the best pre-divide and clock: |
Willie Ruan | 4a0a700 | 2012-01-10 15:39:44 -0800 | [diff] [blame] | 155 | * (PWM Period / N) = (Pre-divide * Clock Period) * 2^m |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 156 | */ |
| 157 | #define NUM_CLOCKS 3 |
| 158 | |
Willie Ruan | db78e94 | 2012-01-10 15:00:28 -0800 | [diff] [blame] | 159 | #define NSEC_1024HZ (NSEC_PER_SEC / 1024) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 160 | #define NSEC_32768HZ (NSEC_PER_SEC / 32768) |
| 161 | #define NSEC_19P2MHZ (NSEC_PER_SEC / 19200000) |
| 162 | |
Willie Ruan | b10be97 | 2012-01-12 11:30:11 -0800 | [diff] [blame] | 163 | #define NUM_LPG_PRE_DIVIDE 4 |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 164 | #define NUM_PWM_PRE_DIVIDE 2 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 165 | |
Jay Chokshi | 0c220cd | 2011-12-09 17:18:20 -0800 | [diff] [blame] | 166 | #define PRE_DIVIDE_1 1 /* v1 */ |
Willie Ruan | b10be97 | 2012-01-12 11:30:11 -0800 | [diff] [blame] | 167 | #define PRE_DIVIDE_2 2 |
| 168 | #define PRE_DIVIDE_3 3 |
| 169 | #define PRE_DIVIDE_5 5 |
| 170 | #define PRE_DIVIDE_6 6 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 171 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 172 | static unsigned int pt_t[NUM_LPG_PRE_DIVIDE][NUM_CLOCKS] = { |
Willie Ruan | db78e94 | 2012-01-10 15:00:28 -0800 | [diff] [blame] | 173 | { PRE_DIVIDE_2 * NSEC_1024HZ, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 174 | PRE_DIVIDE_2 * NSEC_32768HZ, |
| 175 | PRE_DIVIDE_2 * NSEC_19P2MHZ, |
| 176 | }, |
Willie Ruan | b10be97 | 2012-01-12 11:30:11 -0800 | [diff] [blame] | 177 | { PRE_DIVIDE_3 * NSEC_1024HZ, |
| 178 | PRE_DIVIDE_3 * NSEC_32768HZ, |
| 179 | PRE_DIVIDE_3 * NSEC_19P2MHZ, |
| 180 | }, |
| 181 | { PRE_DIVIDE_5 * NSEC_1024HZ, |
| 182 | PRE_DIVIDE_5 * NSEC_32768HZ, |
| 183 | PRE_DIVIDE_5 * NSEC_19P2MHZ, |
| 184 | }, |
| 185 | { PRE_DIVIDE_6 * NSEC_1024HZ, |
| 186 | PRE_DIVIDE_6 * NSEC_32768HZ, |
| 187 | PRE_DIVIDE_6 * NSEC_19P2MHZ, |
| 188 | }, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 189 | }; |
| 190 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 191 | /* Private data */ |
| 192 | struct pm8xxx_pwm_chip; |
| 193 | |
| 194 | struct pwm_device { |
| 195 | int pwm_id; /* = bank/channel id */ |
| 196 | int in_use; |
| 197 | const char *label; |
Willie Ruan | 719c676 | 2011-08-25 11:03:14 -0700 | [diff] [blame] | 198 | struct pm8xxx_pwm_period period; |
| 199 | int pwm_value; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 200 | int pwm_period; |
| 201 | int pwm_duty; |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 202 | u8 pwm_lpg_ctl[PM8XXX_LPG_CTL_REGS]; |
| 203 | u8 pwm_ctl1; |
| 204 | u8 pwm_ctl2; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 205 | int irq; |
| 206 | struct pm8xxx_pwm_chip *chip; |
Willie Ruan | 719c676 | 2011-08-25 11:03:14 -0700 | [diff] [blame] | 207 | int bypass_lut; |
Jay Chokshi | b0a0fa5 | 2012-02-23 16:18:44 -0800 | [diff] [blame^] | 208 | int dtest_mode_supported; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | struct pm8xxx_pwm_chip { |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 212 | struct pwm_device *pwm_dev; |
| 213 | u8 pwm_channels; |
| 214 | u8 pwm_total_pre_divs; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 215 | u8 bank_mask; |
| 216 | struct mutex pwm_mutex; |
| 217 | struct device *dev; |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 218 | bool is_lpg_supported; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | static struct pm8xxx_pwm_chip *pwm_chip; |
| 222 | |
Willie Ruan | 719c676 | 2011-08-25 11:03:14 -0700 | [diff] [blame] | 223 | struct pm8xxx_pwm_lut { |
| 224 | /* LUT parameters */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 225 | int lut_duty_ms; |
| 226 | int lut_lo_index; |
| 227 | int lut_hi_index; |
| 228 | int lut_pause_hi; |
| 229 | int lut_pause_lo; |
| 230 | int flags; |
| 231 | }; |
| 232 | |
| 233 | static const u16 duty_msec[PM8XXX_PWM_1KHZ_COUNT_MAX + 1] = { |
| 234 | 0, 1, 2, 3, 4, 6, 8, 16, 18, 24, 32, 36, 64, 128, 256, 512 |
| 235 | }; |
| 236 | |
| 237 | static const u16 pause_count[PM8XXX_PWM_PAUSE_COUNT_MAX + 1] = { |
| 238 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, |
| 239 | 23, 28, 31, 42, 47, 56, 63, 83, 94, 111, 125, 167, 188, 222, 250, 333, |
| 240 | 375, 500, 667, 750, 800, 900, 1000, 1100, |
| 241 | 1200, 1300, 1400, 1500, 1600, 1800, 2000, 2500, |
| 242 | 3000, 3500, 4000, 4500, 5000, 5500, 6000, 6500, |
| 243 | 7000 |
| 244 | }; |
| 245 | |
| 246 | /* Internal functions */ |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 247 | static void pm8xxx_pwm_save(u8 *u8p, u8 mask, u8 val) |
| 248 | { |
| 249 | *u8p &= ~mask; |
| 250 | *u8p |= val & mask; |
| 251 | } |
| 252 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 253 | static int pm8xxx_pwm_bank_enable(struct pwm_device *pwm, int enable) |
| 254 | { |
| 255 | int rc; |
| 256 | u8 reg; |
| 257 | struct pm8xxx_pwm_chip *chip; |
| 258 | |
| 259 | chip = pwm->chip; |
| 260 | |
| 261 | if (enable) |
| 262 | reg = chip->bank_mask | (1 << pwm->pwm_id); |
| 263 | else |
| 264 | reg = chip->bank_mask & ~(1 << pwm->pwm_id); |
| 265 | |
| 266 | rc = pm8xxx_writeb(chip->dev->parent, SSBI_REG_ADDR_LPG_BANK_EN, reg); |
| 267 | if (rc) { |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 268 | pr_err("pm8xxx_writeb(): rc=%d (Enable LPG Bank)\n", rc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 269 | return rc; |
| 270 | } |
| 271 | chip->bank_mask = reg; |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static int pm8xxx_pwm_bank_sel(struct pwm_device *pwm) |
| 277 | { |
| 278 | int rc; |
| 279 | |
| 280 | rc = pm8xxx_writeb(pwm->chip->dev->parent, SSBI_REG_ADDR_LPG_BANK_SEL, |
| 281 | pwm->pwm_id); |
| 282 | if (rc) |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 283 | pr_err("pm8xxx_writeb(): rc=%d (Select PWM Bank)\n", rc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 284 | return rc; |
| 285 | } |
| 286 | |
| 287 | static int pm8xxx_pwm_start(struct pwm_device *pwm, int start, int ramp_start) |
| 288 | { |
| 289 | int rc; |
| 290 | u8 reg; |
| 291 | |
| 292 | if (start) { |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 293 | reg = pwm->pwm_lpg_ctl[0] | PM8XXX_PWM_PWM_START; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 294 | if (ramp_start) |
| 295 | reg |= PM8XXX_PWM_RAMP_GEN_START; |
| 296 | else |
| 297 | reg &= ~PM8XXX_PWM_RAMP_GEN_START; |
| 298 | } else { |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 299 | reg = pwm->pwm_lpg_ctl[0] & ~PM8XXX_PWM_PWM_START; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 300 | reg &= ~PM8XXX_PWM_RAMP_GEN_START; |
| 301 | } |
| 302 | |
| 303 | rc = pm8xxx_writeb(pwm->chip->dev->parent, SSBI_REG_ADDR_LPG_CTL(0), |
| 304 | reg); |
| 305 | if (rc) |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 306 | pr_err("pm8xxx_writeb(): rc=%d (Enable PWM Ctl 0)\n", rc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 307 | else |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 308 | pwm->pwm_lpg_ctl[0] = reg; |
| 309 | return rc; |
| 310 | } |
| 311 | |
| 312 | static int pm8xxx_pwm_disable(struct pwm_device *pwm) |
| 313 | { |
| 314 | int rc; |
| 315 | u8 reg; |
| 316 | |
| 317 | reg = pwm->pwm_ctl1 & PM8XXX_PWM_DISABLE; |
| 318 | |
| 319 | rc = pm8xxx_writeb(pwm->chip->dev->parent, |
| 320 | SSBI_REG_ADDR_PWM_CTL1(pwm->pwm_id), reg); |
| 321 | |
| 322 | if (rc) |
| 323 | pr_err("pm8xxx_writeb(): rc=%d (Disable PWM Ctl %d)\n", rc, |
| 324 | pwm->pwm_id); |
| 325 | return rc; |
| 326 | } |
| 327 | |
| 328 | static int pm8xxx_pwm_enable(struct pwm_device *pwm) |
| 329 | { |
| 330 | /** |
| 331 | * A kind of best Effort: Just write the clock information that |
| 332 | * we have in the register. |
| 333 | */ |
| 334 | int rc; |
| 335 | |
| 336 | rc = pm8xxx_writeb(pwm->chip->dev->parent, |
| 337 | SSBI_REG_ADDR_PWM_CTL1(pwm->pwm_id), pwm->pwm_ctl1); |
| 338 | |
| 339 | if (rc) |
| 340 | pr_err("pm8xxx_writeb(): rc=%d (Enable PWM Ctl %d)\n", rc, |
| 341 | pwm->pwm_id); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 342 | return rc; |
| 343 | } |
| 344 | |
| 345 | static void pm8xxx_pwm_calc_period(unsigned int period_us, |
Willie Ruan | 719c676 | 2011-08-25 11:03:14 -0700 | [diff] [blame] | 346 | struct pm8xxx_pwm_period *period) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 347 | { |
| 348 | int n, m, clk, div; |
| 349 | int best_m, best_div, best_clk; |
Willie Ruan | 4a0a700 | 2012-01-10 15:39:44 -0800 | [diff] [blame] | 350 | unsigned int last_err, cur_err, min_err; |
| 351 | unsigned int tmp_p, period_n; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 352 | |
| 353 | /* PWM Period / N */ |
Willie Ruan | 70f989d | 2012-01-10 10:09:36 -0800 | [diff] [blame] | 354 | if (period_us < ((unsigned)(-1) / NSEC_PER_USEC)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 355 | period_n = (period_us * NSEC_PER_USEC) >> 6; |
| 356 | n = 6; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 357 | } else { |
| 358 | period_n = (period_us >> 9) * NSEC_PER_USEC; |
| 359 | n = 9; |
| 360 | } |
| 361 | |
Willie Ruan | 4a0a700 | 2012-01-10 15:39:44 -0800 | [diff] [blame] | 362 | min_err = last_err = (unsigned)(-1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 363 | best_m = 0; |
| 364 | best_clk = 0; |
| 365 | best_div = 0; |
| 366 | for (clk = 0; clk < NUM_CLOCKS; clk++) { |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 367 | for (div = 0; div < pwm_chip->pwm_total_pre_divs; div++) { |
Willie Ruan | 4a0a700 | 2012-01-10 15:39:44 -0800 | [diff] [blame] | 368 | /* period_n = (PWM Period / N) */ |
| 369 | /* tmp_p = (Pre-divide * Clock Period) * 2^m */ |
| 370 | tmp_p = pt_t[div][clk]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 371 | for (m = 0; m <= PM8XXX_PWM_M_MAX; m++) { |
Willie Ruan | 4a0a700 | 2012-01-10 15:39:44 -0800 | [diff] [blame] | 372 | if (period_n > tmp_p) |
| 373 | cur_err = period_n - tmp_p; |
| 374 | else |
| 375 | cur_err = tmp_p - period_n; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 376 | |
Willie Ruan | 4a0a700 | 2012-01-10 15:39:44 -0800 | [diff] [blame] | 377 | if (cur_err < min_err) { |
| 378 | min_err = cur_err; |
| 379 | best_m = m; |
| 380 | best_clk = clk; |
| 381 | best_div = div; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 382 | } |
Willie Ruan | 4a0a700 | 2012-01-10 15:39:44 -0800 | [diff] [blame] | 383 | |
| 384 | if (m && cur_err > last_err) |
| 385 | /* Break for bigger cur_err */ |
| 386 | break; |
| 387 | |
| 388 | last_err = cur_err; |
| 389 | tmp_p <<= 1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
Willie Ruan | 75d9e5b | 2011-08-25 10:59:17 -0700 | [diff] [blame] | 394 | /* Use higher resolution */ |
| 395 | if (best_m >= 3 && n == 6) { |
| 396 | n += 3; |
| 397 | best_m -= 3; |
| 398 | } |
| 399 | |
Willie Ruan | 719c676 | 2011-08-25 11:03:14 -0700 | [diff] [blame] | 400 | period->pwm_size = n; |
| 401 | period->clk = best_clk; |
| 402 | period->pre_div = best_div; |
| 403 | period->pre_div_exp = best_m; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 406 | static void pm8xxx_pwm_calc_pwm_value(struct pwm_device *pwm, |
| 407 | unsigned int period_us, |
| 408 | unsigned int duty_us) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 409 | { |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 410 | unsigned int max_pwm_value, tmp; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 411 | |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 412 | /* Figure out pwm_value with overflow handling */ |
| 413 | tmp = 1 << (sizeof(tmp) * 8 - pwm->period.pwm_size); |
| 414 | if (duty_us < tmp) { |
| 415 | tmp = duty_us << pwm->period.pwm_size; |
| 416 | pwm->pwm_value = tmp / period_us; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 417 | } else { |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 418 | tmp = period_us >> pwm->period.pwm_size; |
| 419 | pwm->pwm_value = duty_us / tmp; |
| 420 | } |
| 421 | max_pwm_value = (1 << pwm->period.pwm_size) - 1; |
| 422 | if (pwm->pwm_value > max_pwm_value) |
| 423 | pwm->pwm_value = max_pwm_value; |
| 424 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 425 | |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 426 | static int pm8xxx_pwm_change_table(struct pwm_device *pwm, int duty_pct[], |
| 427 | int start_idx, int len, int raw_value) |
| 428 | { |
| 429 | unsigned int pwm_value, max_pwm_value; |
| 430 | u8 cfg0, cfg1; |
| 431 | int i, pwm_size; |
| 432 | int rc = 0; |
| 433 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 434 | pwm_size = (pwm->pwm_lpg_ctl[5] & PM8XXX_PWM_SIZE_9_BIT) ? 9 : 6; |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 435 | max_pwm_value = (1 << pwm_size) - 1; |
| 436 | for (i = 0; i < len; i++) { |
| 437 | if (raw_value) |
| 438 | pwm_value = duty_pct[i]; |
| 439 | else |
| 440 | pwm_value = (duty_pct[i] << pwm_size) / 100; |
| 441 | |
| 442 | if (pwm_value > max_pwm_value) |
| 443 | pwm_value = max_pwm_value; |
| 444 | cfg0 = pwm_value; |
| 445 | cfg1 = (pwm_value >> 1) & 0x80; |
| 446 | cfg1 |= start_idx + i; |
| 447 | |
| 448 | rc = pm8xxx_writeb(pwm->chip->dev->parent, |
| 449 | SSBI_REG_ADDR_LPG_LUT_CFG0, cfg0); |
| 450 | if (rc) |
| 451 | break; |
| 452 | |
| 453 | rc = pm8xxx_writeb(pwm->chip->dev->parent, |
| 454 | SSBI_REG_ADDR_LPG_LUT_CFG1, cfg1); |
| 455 | if (rc) |
| 456 | break; |
| 457 | } |
| 458 | return rc; |
| 459 | } |
| 460 | |
| 461 | static void pm8xxx_pwm_save_index(struct pwm_device *pwm, |
| 462 | int low_idx, int high_idx, int flags) |
| 463 | { |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 464 | pwm->pwm_lpg_ctl[1] = high_idx & PM8XXX_PWM_HIGH_INDEX_MASK; |
| 465 | pwm->pwm_lpg_ctl[2] = low_idx & PM8XXX_PWM_LOW_INDEX_MASK; |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 466 | |
| 467 | if (flags & PM_PWM_LUT_REVERSE) |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 468 | pwm->pwm_lpg_ctl[1] |= PM8XXX_PWM_REVERSE_EN; |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 469 | if (flags & PM_PWM_LUT_RAMP_UP) |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 470 | pwm->pwm_lpg_ctl[2] |= PM8XXX_PWM_RAMP_UP; |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 471 | if (flags & PM_PWM_LUT_LOOP) |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 472 | pwm->pwm_lpg_ctl[2] |= PM8XXX_PWM_LOOP_EN; |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | static void pm8xxx_pwm_save_period(struct pwm_device *pwm) |
| 476 | { |
| 477 | u8 mask, val; |
| 478 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 479 | if (pwm_chip->is_lpg_supported) { |
| 480 | val = ((pwm->period.clk + 1) << PM8XXX_LPG_PWM_CLK_SEL_SHIFT) |
| 481 | & PM8XXX_LPG_PWM_CLK_SEL_MASK; |
| 482 | val |= (pwm->period.pre_div << PM8XXX_LPG_PWM_PREDIVIDE_SHIFT) |
| 483 | & PM8XXX_LPG_PWM_PREDIVIDE_MASK; |
| 484 | val |= pwm->period.pre_div_exp & PM8XXX_LPG_PWM_M_MASK; |
| 485 | mask = PM8XXX_LPG_PWM_CLK_SEL_MASK | |
| 486 | PM8XXX_LPG_PWM_PREDIVIDE_MASK | PM8XXX_LPG_PWM_M_MASK; |
| 487 | pm8xxx_pwm_save(&pwm->pwm_lpg_ctl[4], mask, val); |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 488 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 489 | val = (pwm->period.pwm_size > 6) ? PM8XXX_PWM_SIZE_9_BIT : 0; |
| 490 | mask = PM8XXX_PWM_SIZE_9_BIT; |
| 491 | pm8xxx_pwm_save(&pwm->pwm_lpg_ctl[5], mask, val); |
| 492 | } else { |
| 493 | val = ((pwm->period.clk + 1) << PM8XXX_PWM_CLK_SEL_SHIFT) |
| 494 | & PM8XXX_PWM_CLK_SEL_MASK; |
| 495 | val |= (pwm->period.pre_div << PM8XXX_PWM_PREDIVIDE_SHIFT) |
| 496 | & PM8XXX_PWM_PREDIVIDE_MASK; |
| 497 | val |= (pwm->period.pre_div_exp << PM8XXX_PWM_M_SHIFT) |
| 498 | & PM8XXX_PWM_M_MASK; |
| 499 | val |= (((pwm->period.pwm_size > 6) ? PM8XXX_PWM_SIZE_9_BIT : 0) |
| 500 | << PM8XXX_PWM_SIZE_SHIFT) & PM8XXX_PWM_SIZE_MASK; |
| 501 | |
| 502 | mask = PM8XXX_PWM_CLK_SEL_MASK | PM8XXX_PWM_PREDIVIDE_MASK | |
| 503 | PM8XXX_PWM_M_MASK | PM8XXX_PWM_SIZE_MASK; |
| 504 | pm8xxx_pwm_save(&pwm->pwm_ctl1, mask, val); |
| 505 | } |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static void pm8xxx_pwm_save_pwm_value(struct pwm_device *pwm) |
| 509 | { |
| 510 | u8 mask, val; |
| 511 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 512 | if (pwm_chip->is_lpg_supported) { |
| 513 | val = (pwm->period.pwm_size > 6) ? (pwm->pwm_value >> 1) : 0; |
| 514 | pwm->pwm_lpg_ctl[3] = pwm->pwm_value; |
| 515 | mask = PM8XXX_PWM_VALUE_BIT8; |
| 516 | pm8xxx_pwm_save(&pwm->pwm_lpg_ctl[4], mask, val); |
| 517 | } else { |
| 518 | val = (pwm->period.pwm_size > 6) ? (pwm->pwm_value >> 8) : 0; |
| 519 | pwm->pwm_ctl2 = pwm->pwm_value; |
| 520 | mask = PM8XXX_PWM_VALUE_BIT0; |
| 521 | pm8xxx_pwm_save(&pwm->pwm_ctl1, mask, val); |
| 522 | } |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | static void pm8xxx_pwm_save_duty_time(struct pwm_device *pwm, |
| 526 | struct pm8xxx_pwm_lut *lut) |
| 527 | { |
| 528 | int i; |
| 529 | u8 mask, val; |
| 530 | |
| 531 | /* Linear search for duty time */ |
| 532 | for (i = 0; i < PM8XXX_PWM_1KHZ_COUNT_MAX; i++) { |
| 533 | if (duty_msec[i] >= lut->lut_duty_ms) |
| 534 | break; |
| 535 | } |
| 536 | val = i << PM8XXX_PWM_1KHZ_COUNT_SHIFT; |
| 537 | |
| 538 | mask = PM8XXX_PWM_1KHZ_COUNT_MASK; |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 539 | pm8xxx_pwm_save(&pwm->pwm_lpg_ctl[0], mask, val); |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | static void pm8xxx_pwm_save_pause(struct pwm_device *pwm, |
| 543 | struct pm8xxx_pwm_lut *lut) |
| 544 | { |
| 545 | int i, pause_cnt, time_cnt; |
| 546 | u8 mask, val; |
| 547 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 548 | time_cnt = (pwm->pwm_lpg_ctl[0] & PM8XXX_PWM_1KHZ_COUNT_MASK) |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 549 | >> PM8XXX_PWM_1KHZ_COUNT_SHIFT; |
| 550 | if (lut->flags & PM_PWM_LUT_PAUSE_HI_EN) { |
| 551 | pause_cnt = (lut->lut_pause_hi + duty_msec[time_cnt] / 2) |
| 552 | / duty_msec[time_cnt]; |
| 553 | /* Linear search for pause time */ |
| 554 | for (i = 0; i < PM8XXX_PWM_PAUSE_COUNT_MAX; i++) { |
| 555 | if (pause_count[i] >= pause_cnt) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 556 | break; |
| 557 | } |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 558 | val = (i << PM8XXX_PWM_PAUSE_COUNT_HI_SHIFT) & |
| 559 | PM8XXX_PWM_PAUSE_COUNT_HI_MASK; |
| 560 | val |= PM8XXX_PWM_PAUSE_ENABLE_HIGH; |
| 561 | } else { |
| 562 | val = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 565 | mask = PM8XXX_PWM_PAUSE_COUNT_HI_MASK | PM8XXX_PWM_PAUSE_ENABLE_HIGH; |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 566 | pm8xxx_pwm_save(&pwm->pwm_lpg_ctl[5], mask, val); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 567 | |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 568 | if (lut->flags & PM_PWM_LUT_PAUSE_LO_EN) { |
| 569 | /* Linear search for pause time */ |
| 570 | pause_cnt = (lut->lut_pause_lo + duty_msec[time_cnt] / 2) |
| 571 | / duty_msec[time_cnt]; |
| 572 | for (i = 0; i < PM8XXX_PWM_PAUSE_COUNT_MAX; i++) { |
| 573 | if (pause_count[i] >= pause_cnt) |
| 574 | break; |
| 575 | } |
| 576 | val = (i << PM8XXX_PWM_PAUSE_COUNT_LO_SHIFT) & |
| 577 | PM8XXX_PWM_PAUSE_COUNT_LO_MASK; |
| 578 | val |= PM8XXX_PWM_PAUSE_ENABLE_LOW; |
| 579 | } else { |
| 580 | val = 0; |
| 581 | } |
| 582 | |
| 583 | mask = PM8XXX_PWM_PAUSE_COUNT_LO_MASK | PM8XXX_PWM_PAUSE_ENABLE_LOW; |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 584 | pm8xxx_pwm_save(&pwm->pwm_lpg_ctl[6], mask, val); |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 587 | static int pm8xxx_pwm_write(struct pwm_device *pwm) |
| 588 | { |
| 589 | int rc = 0; |
| 590 | |
| 591 | rc = pm8xxx_writeb(pwm->chip->dev->parent, |
| 592 | SSBI_REG_ADDR_PWM_CTL1(pwm->pwm_id), |
| 593 | pwm->pwm_ctl1); |
| 594 | if (rc) { |
| 595 | pr_err("pm8xxx_writeb() failed: rc=%d (PWM Ctl1[%d])\n", |
| 596 | rc, pwm->pwm_id); |
| 597 | return rc; |
| 598 | } |
| 599 | |
| 600 | rc = pm8xxx_writeb(pwm->chip->dev->parent, |
| 601 | SSBI_REG_ADDR_PWM_CTL2(pwm->pwm_id), |
| 602 | pwm->pwm_ctl2); |
| 603 | if (rc) { |
| 604 | pr_err("pm8xxx_writeb() failed: rc=%d (PWM Ctl2[%d])\n", |
| 605 | rc, pwm->pwm_id); |
| 606 | return rc; |
| 607 | } |
| 608 | |
| 609 | return rc; |
| 610 | } |
| 611 | |
| 612 | static int pm8xxx_lpg_pwm_write(struct pwm_device *pwm, int start, int end) |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 613 | { |
| 614 | int i, rc; |
| 615 | |
| 616 | /* Write in reverse way so 0 would be the last */ |
| 617 | for (i = end - 1; i >= start; i--) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 618 | rc = pm8xxx_writeb(pwm->chip->dev->parent, |
| 619 | SSBI_REG_ADDR_LPG_CTL(i), |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 620 | pwm->pwm_lpg_ctl[i]); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 621 | if (rc) { |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 622 | pr_err("pm8xxx_writeb(): rc=%d (PWM Ctl[%d])\n", rc, i); |
| 623 | return rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | static int pm8xxx_pwm_change_lut(struct pwm_device *pwm, |
| 631 | struct pm8xxx_pwm_lut *lut) |
| 632 | { |
| 633 | int rc; |
| 634 | |
| 635 | pm8xxx_pwm_save_index(pwm, lut->lut_lo_index, |
| 636 | lut->lut_hi_index, lut->flags); |
| 637 | pm8xxx_pwm_save_duty_time(pwm, lut); |
| 638 | pm8xxx_pwm_save_pause(pwm, lut); |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 639 | pm8xxx_pwm_save(&pwm->pwm_lpg_ctl[1], PM8XXX_PWM_BYPASS_LUT, 0); |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 640 | |
| 641 | pm8xxx_pwm_bank_sel(pwm); |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 642 | rc = pm8xxx_lpg_pwm_write(pwm, 0, 7); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 643 | |
| 644 | return rc; |
| 645 | } |
| 646 | |
Jay Chokshi | b0a0fa5 | 2012-02-23 16:18:44 -0800 | [diff] [blame^] | 647 | static int pm8xxx_pwm_set_dtest(struct pwm_device *pwm, int enable) |
| 648 | { |
| 649 | int rc; |
| 650 | u8 reg; |
| 651 | |
| 652 | reg = pwm->pwm_id & PM8XXX_PWM_DTEST_BANK_MASK; |
| 653 | |
| 654 | if (enable) { |
| 655 | /* Observe LPG_OUT on DTEST1*/ |
| 656 | reg |= (1 << PM8XXX_PWM_DTEST_SHIFT) & |
| 657 | PM8XXX_PWM_DTEST_MASK; |
| 658 | } |
| 659 | |
| 660 | rc = pm8xxx_writeb(pwm->chip->dev->parent, |
| 661 | SSBI_REG_ADDR_LPG_TEST, reg); |
| 662 | if (rc) |
| 663 | pr_err("pm8xxx_write(DTEST=0x%x) failed: rc=%d\n", |
| 664 | reg, rc); |
| 665 | |
| 666 | return rc; |
| 667 | } |
| 668 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 669 | /* APIs */ |
| 670 | /** |
| 671 | * pwm_request - request a PWM device |
| 672 | * @pwm_id: PWM id or channel |
| 673 | * @label: the label to identify the user |
| 674 | */ |
| 675 | struct pwm_device *pwm_request(int pwm_id, const char *label) |
| 676 | { |
| 677 | struct pwm_device *pwm; |
| 678 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 679 | if (pwm_chip == NULL) { |
| 680 | pr_err("No pwm_chip\n"); |
| 681 | return ERR_PTR(-ENODEV); |
| 682 | } |
| 683 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 684 | if (pwm_id >= pwm_chip->pwm_channels || pwm_id < 0) { |
| 685 | pr_err("Invalid pwm_id: %d with %s\n", |
| 686 | pwm_id, label ? label : "."); |
| 687 | return ERR_PTR(-EINVAL); |
| 688 | } |
| 689 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 690 | mutex_lock(&pwm_chip->pwm_mutex); |
| 691 | pwm = &pwm_chip->pwm_dev[pwm_id]; |
| 692 | if (!pwm->in_use) { |
| 693 | pwm->in_use = 1; |
| 694 | pwm->label = label; |
| 695 | } else { |
| 696 | pwm = ERR_PTR(-EBUSY); |
| 697 | } |
| 698 | mutex_unlock(&pwm_chip->pwm_mutex); |
| 699 | |
| 700 | return pwm; |
| 701 | } |
| 702 | EXPORT_SYMBOL_GPL(pwm_request); |
| 703 | |
| 704 | /** |
| 705 | * pwm_free - free a PWM device |
| 706 | * @pwm: the PWM device |
| 707 | */ |
| 708 | void pwm_free(struct pwm_device *pwm) |
| 709 | { |
| 710 | if (pwm == NULL || IS_ERR(pwm) || pwm->chip == NULL) { |
| 711 | pr_err("Invalid pwm handle\n"); |
| 712 | return; |
| 713 | } |
| 714 | |
| 715 | mutex_lock(&pwm->chip->pwm_mutex); |
| 716 | if (pwm->in_use) { |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 717 | if (pwm_chip->is_lpg_supported) { |
| 718 | pm8xxx_pwm_bank_sel(pwm); |
| 719 | pm8xxx_pwm_start(pwm, 0, 0); |
| 720 | } else { |
| 721 | pm8xxx_pwm_disable(pwm); |
| 722 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 723 | pwm->in_use = 0; |
| 724 | pwm->label = NULL; |
| 725 | } |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 726 | if (pwm_chip->is_lpg_supported) |
| 727 | pm8xxx_pwm_bank_enable(pwm, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 728 | mutex_unlock(&pwm->chip->pwm_mutex); |
| 729 | } |
| 730 | EXPORT_SYMBOL_GPL(pwm_free); |
| 731 | |
| 732 | /** |
| 733 | * pwm_config - change a PWM device configuration |
| 734 | * @pwm: the PWM device |
| 735 | * @period_us: period in microseconds |
| 736 | * @duty_us: duty cycle in microseconds |
| 737 | */ |
| 738 | int pwm_config(struct pwm_device *pwm, int duty_us, int period_us) |
| 739 | { |
Willie Ruan | 719c676 | 2011-08-25 11:03:14 -0700 | [diff] [blame] | 740 | struct pm8xxx_pwm_period *period; |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 741 | int rc = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 742 | |
| 743 | if (pwm == NULL || IS_ERR(pwm) || |
| 744 | duty_us > period_us || |
| 745 | (unsigned)period_us > PM8XXX_PWM_PERIOD_MAX || |
| 746 | (unsigned)period_us < PM8XXX_PWM_PERIOD_MIN) { |
| 747 | pr_err("Invalid pwm handle or parameters\n"); |
| 748 | return -EINVAL; |
| 749 | } |
| 750 | if (pwm->chip == NULL) { |
| 751 | pr_err("No pwm_chip\n"); |
| 752 | return -ENODEV; |
| 753 | } |
| 754 | |
Willie Ruan | 719c676 | 2011-08-25 11:03:14 -0700 | [diff] [blame] | 755 | period = &pwm->period; |
| 756 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 757 | mutex_lock(&pwm->chip->pwm_mutex); |
| 758 | |
| 759 | if (!pwm->in_use) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 760 | rc = -EINVAL; |
| 761 | goto out_unlock; |
| 762 | } |
| 763 | |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 764 | if (pwm->pwm_period != period_us) { |
| 765 | pm8xxx_pwm_calc_period(period_us, period); |
| 766 | pm8xxx_pwm_save_period(pwm); |
| 767 | pwm->pwm_period = period_us; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 768 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 769 | |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 770 | pm8xxx_pwm_calc_pwm_value(pwm, period_us, duty_us); |
| 771 | pm8xxx_pwm_save_pwm_value(pwm); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 772 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 773 | if (pwm_chip->is_lpg_supported) { |
| 774 | pm8xxx_pwm_save(&pwm->pwm_lpg_ctl[1], |
| 775 | PM8XXX_PWM_BYPASS_LUT, PM8XXX_PWM_BYPASS_LUT); |
| 776 | |
| 777 | pm8xxx_pwm_bank_sel(pwm); |
| 778 | rc = pm8xxx_lpg_pwm_write(pwm, 1, 6); |
| 779 | } else { |
| 780 | rc = pm8xxx_pwm_write(pwm); |
| 781 | } |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 782 | |
| 783 | pr_debug("duty/period=%u/%u usec: pwm_value=%d (of %d)\n", |
| 784 | (unsigned)duty_us, (unsigned)period_us, |
| 785 | pwm->pwm_value, 1 << period->pwm_size); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 786 | |
| 787 | out_unlock: |
| 788 | mutex_unlock(&pwm->chip->pwm_mutex); |
| 789 | return rc; |
| 790 | } |
| 791 | EXPORT_SYMBOL_GPL(pwm_config); |
| 792 | |
| 793 | /** |
| 794 | * pwm_enable - start a PWM output toggling |
| 795 | * @pwm: the PWM device |
| 796 | */ |
| 797 | int pwm_enable(struct pwm_device *pwm) |
| 798 | { |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 799 | int rc = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 800 | |
| 801 | if (pwm == NULL || IS_ERR(pwm)) { |
| 802 | pr_err("Invalid pwm handle\n"); |
| 803 | return -EINVAL; |
| 804 | } |
| 805 | if (pwm->chip == NULL) { |
| 806 | pr_err("No pwm_chip\n"); |
| 807 | return -ENODEV; |
| 808 | } |
| 809 | |
| 810 | mutex_lock(&pwm->chip->pwm_mutex); |
| 811 | if (!pwm->in_use) { |
| 812 | pr_err("pwm_id: %d: stale handle?\n", pwm->pwm_id); |
| 813 | rc = -EINVAL; |
| 814 | } else { |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 815 | if (pwm_chip->is_lpg_supported) { |
Jay Chokshi | b0a0fa5 | 2012-02-23 16:18:44 -0800 | [diff] [blame^] | 816 | if (pwm->dtest_mode_supported) |
| 817 | pm8xxx_pwm_set_dtest(pwm, 1); |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 818 | rc = pm8xxx_pwm_bank_enable(pwm, 1); |
| 819 | pm8xxx_pwm_bank_sel(pwm); |
| 820 | pm8xxx_pwm_start(pwm, 1, 0); |
| 821 | } else { |
| 822 | pm8xxx_pwm_enable(pwm); |
| 823 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 824 | } |
| 825 | mutex_unlock(&pwm->chip->pwm_mutex); |
| 826 | return rc; |
| 827 | } |
| 828 | EXPORT_SYMBOL_GPL(pwm_enable); |
| 829 | |
| 830 | /** |
| 831 | * pwm_disable - stop a PWM output toggling |
| 832 | * @pwm: the PWM device |
| 833 | */ |
| 834 | void pwm_disable(struct pwm_device *pwm) |
| 835 | { |
| 836 | if (pwm == NULL || IS_ERR(pwm) || pwm->chip == NULL) { |
| 837 | pr_err("Invalid pwm handle or no pwm_chip\n"); |
| 838 | return; |
| 839 | } |
| 840 | |
| 841 | mutex_lock(&pwm->chip->pwm_mutex); |
| 842 | if (pwm->in_use) { |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 843 | if (pwm_chip->is_lpg_supported) { |
Jay Chokshi | b0a0fa5 | 2012-02-23 16:18:44 -0800 | [diff] [blame^] | 844 | if (pwm->dtest_mode_supported) |
| 845 | pm8xxx_pwm_set_dtest(pwm, 0); |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 846 | pm8xxx_pwm_bank_sel(pwm); |
| 847 | pm8xxx_pwm_start(pwm, 0, 0); |
| 848 | pm8xxx_pwm_bank_enable(pwm, 0); |
| 849 | } else { |
| 850 | pm8xxx_pwm_disable(pwm); |
| 851 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 852 | } |
| 853 | mutex_unlock(&pwm->chip->pwm_mutex); |
| 854 | } |
| 855 | EXPORT_SYMBOL_GPL(pwm_disable); |
| 856 | |
| 857 | /** |
Jay Chokshi | 4acbdd5 | 2011-09-16 17:09:44 -0700 | [diff] [blame] | 858 | * pm8xxx_pwm_config_period - change PWM period |
| 859 | * |
| 860 | * @pwm: the PWM device |
| 861 | * @pwm_p: period in struct pm8xxx_pwm_period |
| 862 | */ |
| 863 | int pm8xxx_pwm_config_period(struct pwm_device *pwm, |
| 864 | struct pm8xxx_pwm_period *period) |
| 865 | { |
| 866 | int rc; |
| 867 | |
| 868 | if (pwm == NULL || IS_ERR(pwm) || period == NULL) |
| 869 | return -EINVAL; |
| 870 | if (pwm->chip == NULL) |
| 871 | return -ENODEV; |
| 872 | |
| 873 | mutex_lock(&pwm->chip->pwm_mutex); |
| 874 | |
| 875 | if (!pwm->in_use) { |
| 876 | rc = -EINVAL; |
| 877 | goto out_unlock; |
| 878 | } |
| 879 | |
| 880 | pwm->period.pwm_size = period->pwm_size; |
| 881 | pwm->period.clk = period->clk; |
| 882 | pwm->period.pre_div = period->pre_div; |
| 883 | pwm->period.pre_div_exp = period->pre_div_exp; |
| 884 | |
| 885 | pm8xxx_pwm_save_period(pwm); |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 886 | |
| 887 | if (pwm_chip->is_lpg_supported) { |
| 888 | pm8xxx_pwm_bank_sel(pwm); |
| 889 | rc = pm8xxx_lpg_pwm_write(pwm, 4, 6); |
| 890 | } else { |
| 891 | rc = pm8xxx_pwm_write(pwm); |
| 892 | } |
| 893 | |
Jay Chokshi | 4acbdd5 | 2011-09-16 17:09:44 -0700 | [diff] [blame] | 894 | |
| 895 | out_unlock: |
| 896 | mutex_unlock(&pwm->chip->pwm_mutex); |
| 897 | return rc; |
| 898 | } |
| 899 | EXPORT_SYMBOL(pm8xxx_pwm_config_period); |
| 900 | |
| 901 | /** |
| 902 | * pm8xxx_pwm_config_pwm_value - change a PWM device configuration |
| 903 | * @pwm: the PWM device |
| 904 | * @pwm_value: the duty cycle in raw PWM value (< 2^pwm_size) |
| 905 | */ |
| 906 | int pm8xxx_pwm_config_pwm_value(struct pwm_device *pwm, int pwm_value) |
| 907 | { |
| 908 | int rc = 0; |
| 909 | |
| 910 | if (pwm == NULL || IS_ERR(pwm)) |
| 911 | return -EINVAL; |
| 912 | if (pwm->chip == NULL) |
| 913 | return -ENODEV; |
| 914 | |
| 915 | mutex_lock(&pwm->chip->pwm_mutex); |
| 916 | |
| 917 | if (!pwm->in_use || !pwm->pwm_period) { |
| 918 | rc = -EINVAL; |
| 919 | goto out_unlock; |
| 920 | } |
| 921 | |
| 922 | if (pwm->pwm_value == pwm_value) |
| 923 | goto out_unlock; |
| 924 | |
| 925 | pwm->pwm_value = pwm_value; |
| 926 | |
| 927 | pm8xxx_pwm_save_pwm_value(pwm); |
Jay Chokshi | 4acbdd5 | 2011-09-16 17:09:44 -0700 | [diff] [blame] | 928 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 929 | if (pwm_chip->is_lpg_supported) { |
| 930 | pm8xxx_pwm_save(&pwm->pwm_lpg_ctl[1], |
| 931 | PM8XXX_PWM_BYPASS_LUT, PM8XXX_PWM_BYPASS_LUT); |
| 932 | pm8xxx_pwm_bank_sel(pwm); |
| 933 | rc = pm8xxx_lpg_pwm_write(pwm, 1, 6); |
| 934 | } else { |
| 935 | rc = pm8xxx_pwm_write(pwm); |
| 936 | } |
Jay Chokshi | 4acbdd5 | 2011-09-16 17:09:44 -0700 | [diff] [blame] | 937 | |
| 938 | if (rc) |
| 939 | pr_err("[%d]: pm8xxx_pwm_write: rc=%d\n", pwm->pwm_id, rc); |
| 940 | |
| 941 | out_unlock: |
| 942 | mutex_unlock(&pwm->chip->pwm_mutex); |
| 943 | return rc; |
| 944 | } |
| 945 | EXPORT_SYMBOL_GPL(pm8xxx_pwm_config_pwm_value); |
| 946 | |
| 947 | /** |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 948 | * pm8xxx_pwm_lut_config - change a PWM device configuration to use LUT |
| 949 | * @pwm: the PWM device |
| 950 | * @period_us: period in microseconds |
| 951 | * @duty_pct: arrary of duty cycles in percent, like 20, 50. |
| 952 | * @duty_time_ms: time for each duty cycle in milliseconds |
| 953 | * @start_idx: start index in lookup table from 0 to MAX-1 |
| 954 | * @idx_len: number of index |
| 955 | * @pause_lo: pause time in milliseconds at low index |
| 956 | * @pause_hi: pause time in milliseconds at high index |
| 957 | * @flags: control flags |
| 958 | */ |
| 959 | int pm8xxx_pwm_lut_config(struct pwm_device *pwm, int period_us, |
| 960 | int duty_pct[], int duty_time_ms, int start_idx, |
| 961 | int idx_len, int pause_lo, int pause_hi, int flags) |
| 962 | { |
Willie Ruan | 719c676 | 2011-08-25 11:03:14 -0700 | [diff] [blame] | 963 | struct pm8xxx_pwm_lut lut; |
| 964 | struct pm8xxx_pwm_period *period; |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 965 | int len; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 966 | int rc; |
| 967 | |
| 968 | if (pwm == NULL || IS_ERR(pwm) || !idx_len) { |
| 969 | pr_err("Invalid pwm handle or idx_len=0\n"); |
| 970 | return -EINVAL; |
| 971 | } |
| 972 | if (duty_pct == NULL && !(flags & PM_PWM_LUT_NO_TABLE)) { |
| 973 | pr_err("Invalid duty_pct with flag\n"); |
| 974 | return -EINVAL; |
| 975 | } |
| 976 | if (pwm->chip == NULL) { |
| 977 | pr_err("No pwm_chip\n"); |
| 978 | return -ENODEV; |
| 979 | } |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 980 | |
| 981 | if (pwm->chip->is_lpg_supported == 0) { |
| 982 | pr_err("LPG module isn't supported\n"); |
| 983 | return -EINVAL; |
| 984 | } |
| 985 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 986 | if (idx_len >= PM_PWM_LUT_SIZE && start_idx) { |
| 987 | pr_err("Wrong LUT size or index\n"); |
| 988 | return -EINVAL; |
| 989 | } |
| 990 | if ((start_idx + idx_len) > PM_PWM_LUT_SIZE) { |
| 991 | pr_err("Exceed LUT limit\n"); |
| 992 | return -EINVAL; |
| 993 | } |
| 994 | if ((unsigned)period_us > PM8XXX_PWM_PERIOD_MAX || |
| 995 | (unsigned)period_us < PM8XXX_PWM_PERIOD_MIN) { |
| 996 | pr_err("Period out of range\n"); |
| 997 | return -EINVAL; |
| 998 | } |
| 999 | |
Willie Ruan | 719c676 | 2011-08-25 11:03:14 -0700 | [diff] [blame] | 1000 | period = &pwm->period; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1001 | mutex_lock(&pwm->chip->pwm_mutex); |
| 1002 | |
| 1003 | if (!pwm->in_use) { |
| 1004 | pr_err("pwm_id: %d: stale handle?\n", pwm->pwm_id); |
| 1005 | rc = -EINVAL; |
| 1006 | goto out_unlock; |
| 1007 | } |
| 1008 | |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 1009 | if (pwm->pwm_period != period_us) { |
| 1010 | pm8xxx_pwm_calc_period(period_us, period); |
| 1011 | pm8xxx_pwm_save_period(pwm); |
| 1012 | pwm->pwm_period = period_us; |
| 1013 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1014 | |
| 1015 | len = (idx_len > PM_PWM_LUT_SIZE) ? PM_PWM_LUT_SIZE : idx_len; |
| 1016 | |
| 1017 | if (flags & PM_PWM_LUT_NO_TABLE) |
| 1018 | goto after_table_write; |
| 1019 | |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 1020 | rc = pm8xxx_pwm_change_table(pwm, duty_pct, start_idx, len, 0); |
| 1021 | if (rc) { |
| 1022 | pr_err("pm8xxx_pwm_change_table: rc=%d\n", rc); |
| 1023 | goto out_unlock; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | after_table_write: |
Willie Ruan | 719c676 | 2011-08-25 11:03:14 -0700 | [diff] [blame] | 1027 | lut.lut_duty_ms = duty_time_ms; |
| 1028 | lut.lut_lo_index = start_idx; |
| 1029 | lut.lut_hi_index = start_idx + len - 1; |
| 1030 | lut.lut_pause_lo = pause_lo; |
| 1031 | lut.lut_pause_hi = pause_hi; |
| 1032 | lut.flags = flags; |
| 1033 | pwm->bypass_lut = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1034 | |
Willie Ruan | 8de2f38 | 2011-08-25 11:04:50 -0700 | [diff] [blame] | 1035 | rc = pm8xxx_pwm_change_lut(pwm, &lut); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1036 | |
| 1037 | out_unlock: |
| 1038 | mutex_unlock(&pwm->chip->pwm_mutex); |
| 1039 | return rc; |
| 1040 | } |
| 1041 | EXPORT_SYMBOL_GPL(pm8xxx_pwm_lut_config); |
| 1042 | |
| 1043 | /** |
| 1044 | * pm8xxx_pwm_lut_enable - control a PWM device to start/stop LUT ramp |
| 1045 | * @pwm: the PWM device |
| 1046 | * @start: to start (1), or stop (0) |
| 1047 | */ |
| 1048 | int pm8xxx_pwm_lut_enable(struct pwm_device *pwm, int start) |
| 1049 | { |
| 1050 | if (pwm == NULL || IS_ERR(pwm)) { |
| 1051 | pr_err("Invalid pwm handle\n"); |
| 1052 | return -EINVAL; |
| 1053 | } |
| 1054 | if (pwm->chip == NULL) { |
| 1055 | pr_err("No pwm_chip\n"); |
| 1056 | return -ENODEV; |
| 1057 | } |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 1058 | if (pwm->chip->is_lpg_supported == 0) { |
| 1059 | pr_err("LPG module isn't supported\n"); |
| 1060 | return -EINVAL; |
| 1061 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1062 | |
| 1063 | mutex_lock(&pwm->chip->pwm_mutex); |
| 1064 | if (start) { |
Jay Chokshi | b0a0fa5 | 2012-02-23 16:18:44 -0800 | [diff] [blame^] | 1065 | if (pwm->dtest_mode_supported) |
| 1066 | pm8xxx_pwm_set_dtest(pwm, 1); |
| 1067 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1068 | pm8xxx_pwm_bank_enable(pwm, 1); |
| 1069 | |
| 1070 | pm8xxx_pwm_bank_sel(pwm); |
| 1071 | pm8xxx_pwm_start(pwm, 1, 1); |
| 1072 | } else { |
Jay Chokshi | b0a0fa5 | 2012-02-23 16:18:44 -0800 | [diff] [blame^] | 1073 | if (pwm->dtest_mode_supported) |
| 1074 | pm8xxx_pwm_set_dtest(pwm, 0); |
| 1075 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1076 | pm8xxx_pwm_bank_sel(pwm); |
| 1077 | pm8xxx_pwm_start(pwm, 0, 0); |
| 1078 | |
| 1079 | pm8xxx_pwm_bank_enable(pwm, 0); |
| 1080 | } |
| 1081 | mutex_unlock(&pwm->chip->pwm_mutex); |
| 1082 | return 0; |
| 1083 | } |
| 1084 | EXPORT_SYMBOL_GPL(pm8xxx_pwm_lut_enable); |
| 1085 | |
| 1086 | #if defined(CONFIG_DEBUG_FS) |
| 1087 | |
| 1088 | struct pm8xxx_pwm_dbg_device; |
| 1089 | |
| 1090 | struct pm8xxx_pwm_user { |
| 1091 | int pwm_id; |
| 1092 | struct pwm_device *pwm; |
| 1093 | int period; |
| 1094 | int duty_cycle; |
| 1095 | int enable; |
| 1096 | struct pm8xxx_pwm_dbg_device *dbgdev; |
| 1097 | }; |
| 1098 | |
| 1099 | struct pm8xxx_pwm_dbg_device { |
| 1100 | struct mutex dbg_mutex; |
| 1101 | struct device *dev; |
| 1102 | struct dentry *dent; |
| 1103 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 1104 | struct pm8xxx_pwm_user *user; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1105 | }; |
| 1106 | |
| 1107 | static struct pm8xxx_pwm_dbg_device *pmic_dbg_device; |
| 1108 | |
| 1109 | static int dbg_pwm_check_period(int period) |
| 1110 | { |
| 1111 | if (period < PM8XXX_PWM_PERIOD_MIN || period > PM8XXX_PWM_PERIOD_MAX) { |
| 1112 | pr_err("period is invalid: %d\n", period); |
| 1113 | return -EINVAL; |
| 1114 | } |
| 1115 | return 0; |
| 1116 | } |
| 1117 | |
| 1118 | static int dbg_pwm_check_duty_cycle(int duty_cycle, const char *func_name) |
| 1119 | { |
| 1120 | if (duty_cycle <= 0 || duty_cycle > 100) { |
| 1121 | pr_err("%s: duty_cycle is invalid: %d\n", |
| 1122 | func_name, duty_cycle); |
| 1123 | return -EINVAL; |
| 1124 | } |
| 1125 | return 0; |
| 1126 | } |
| 1127 | |
| 1128 | static void dbg_pwm_check_handle(struct pm8xxx_pwm_user *puser) |
| 1129 | { |
| 1130 | struct pwm_device *tmp; |
| 1131 | |
| 1132 | if (puser->pwm == NULL) { |
| 1133 | tmp = pwm_request(puser->pwm_id, "pwm-dbg"); |
| 1134 | if (PTR_ERR(puser->pwm)) { |
| 1135 | pr_err("pwm_request: err=%ld\n", PTR_ERR(puser->pwm)); |
| 1136 | puser->pwm = NULL; |
Willie Ruan | 10976ea | 2011-08-25 11:01:15 -0700 | [diff] [blame] | 1137 | } else { |
| 1138 | pr_debug("[id=%d] pwm_request ok\n", puser->pwm_id); |
| 1139 | puser->pwm = tmp; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | static int dbg_pwm_enable_set(void *data, u64 val) |
| 1145 | { |
| 1146 | struct pm8xxx_pwm_user *puser = data; |
| 1147 | struct pm8xxx_pwm_dbg_device *dbgdev = puser->dbgdev; |
| 1148 | int rc; |
| 1149 | |
| 1150 | mutex_lock(&dbgdev->dbg_mutex); |
| 1151 | rc = dbg_pwm_check_duty_cycle(puser->duty_cycle, __func__); |
| 1152 | if (!rc) { |
| 1153 | puser->enable = val; |
| 1154 | dbg_pwm_check_handle(puser); |
| 1155 | if (puser->pwm) { |
| 1156 | if (puser->enable) |
| 1157 | pwm_enable(puser->pwm); |
| 1158 | else |
| 1159 | pwm_disable(puser->pwm); |
| 1160 | } |
| 1161 | } |
| 1162 | mutex_unlock(&dbgdev->dbg_mutex); |
| 1163 | return 0; |
| 1164 | } |
| 1165 | |
| 1166 | static int dbg_pwm_enable_get(void *data, u64 *val) |
| 1167 | { |
| 1168 | struct pm8xxx_pwm_user *puser = data; |
| 1169 | struct pm8xxx_pwm_dbg_device *dbgdev = puser->dbgdev; |
| 1170 | |
| 1171 | mutex_lock(&dbgdev->dbg_mutex); |
| 1172 | *val = puser->enable; |
| 1173 | mutex_unlock(&dbgdev->dbg_mutex); |
| 1174 | return 0; |
| 1175 | } |
| 1176 | |
| 1177 | DEFINE_SIMPLE_ATTRIBUTE(dbg_pwm_enable_fops, |
| 1178 | dbg_pwm_enable_get, dbg_pwm_enable_set, |
| 1179 | "%lld\n"); |
| 1180 | |
| 1181 | static int dbg_pwm_duty_cycle_set(void *data, u64 val) |
| 1182 | { |
| 1183 | struct pm8xxx_pwm_user *puser = data; |
| 1184 | struct pm8xxx_pwm_dbg_device *dbgdev = puser->dbgdev; |
| 1185 | int rc; |
| 1186 | |
| 1187 | mutex_lock(&dbgdev->dbg_mutex); |
| 1188 | rc = dbg_pwm_check_duty_cycle(val, __func__); |
| 1189 | if (!rc) { |
| 1190 | puser->duty_cycle = val; |
| 1191 | dbg_pwm_check_handle(puser); |
| 1192 | if (puser->pwm) { |
| 1193 | int duty_us; |
| 1194 | |
Willie Ruan | 10976ea | 2011-08-25 11:01:15 -0700 | [diff] [blame] | 1195 | duty_us = puser->duty_cycle * puser->period / 100; |
| 1196 | pwm_config(puser->pwm, duty_us, puser->period); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1197 | } |
| 1198 | } |
| 1199 | mutex_unlock(&dbgdev->dbg_mutex); |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
| 1203 | static int dbg_pwm_duty_cycle_get(void *data, u64 *val) |
| 1204 | { |
| 1205 | struct pm8xxx_pwm_user *puser = data; |
| 1206 | struct pm8xxx_pwm_dbg_device *dbgdev = puser->dbgdev; |
| 1207 | |
| 1208 | mutex_lock(&dbgdev->dbg_mutex); |
| 1209 | *val = puser->duty_cycle; |
| 1210 | mutex_unlock(&dbgdev->dbg_mutex); |
| 1211 | return 0; |
| 1212 | } |
| 1213 | |
| 1214 | DEFINE_SIMPLE_ATTRIBUTE(dbg_pwm_duty_cycle_fops, |
| 1215 | dbg_pwm_duty_cycle_get, dbg_pwm_duty_cycle_set, |
| 1216 | "%lld\n"); |
| 1217 | |
| 1218 | static int dbg_pwm_period_set(void *data, u64 val) |
| 1219 | { |
| 1220 | struct pm8xxx_pwm_user *puser = data; |
| 1221 | struct pm8xxx_pwm_dbg_device *dbgdev = puser->dbgdev; |
| 1222 | int rc; |
| 1223 | |
| 1224 | mutex_lock(&dbgdev->dbg_mutex); |
| 1225 | rc = dbg_pwm_check_period(val); |
| 1226 | if (!rc) |
| 1227 | puser->period = val; |
| 1228 | mutex_unlock(&dbgdev->dbg_mutex); |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
| 1232 | static int dbg_pwm_period_get(void *data, u64 *val) |
| 1233 | { |
| 1234 | struct pm8xxx_pwm_user *puser = data; |
| 1235 | struct pm8xxx_pwm_dbg_device *dbgdev = puser->dbgdev; |
| 1236 | |
| 1237 | mutex_lock(&dbgdev->dbg_mutex); |
| 1238 | *val = puser->period; |
| 1239 | mutex_unlock(&dbgdev->dbg_mutex); |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
| 1243 | DEFINE_SIMPLE_ATTRIBUTE(dbg_pwm_period_fops, |
| 1244 | dbg_pwm_period_get, dbg_pwm_period_set, "%lld\n"); |
| 1245 | |
| 1246 | static int __devinit pm8xxx_pwm_dbg_probe(struct device *dev) |
| 1247 | { |
| 1248 | struct pm8xxx_pwm_dbg_device *dbgdev; |
| 1249 | struct dentry *dent; |
| 1250 | struct dentry *temp; |
| 1251 | struct pm8xxx_pwm_user *puser; |
| 1252 | int i; |
Jay Chokshi | 00c07cc | 2011-10-27 15:50:03 -0700 | [diff] [blame] | 1253 | int rc = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1254 | |
| 1255 | if (dev == NULL) { |
| 1256 | pr_err("no parent data passed in.\n"); |
| 1257 | return -EINVAL; |
| 1258 | } |
| 1259 | |
| 1260 | dbgdev = kzalloc(sizeof *dbgdev, GFP_KERNEL); |
| 1261 | if (dbgdev == NULL) { |
| 1262 | pr_err("kzalloc() failed.\n"); |
| 1263 | return -ENOMEM; |
| 1264 | } |
| 1265 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 1266 | dbgdev->user = kcalloc(pwm_chip->pwm_channels, |
| 1267 | sizeof(struct pm8xxx_pwm_user), GFP_KERNEL); |
| 1268 | if (dbgdev->user == NULL) { |
| 1269 | pr_err("kcalloc() failed.\n"); |
Jay Chokshi | 00c07cc | 2011-10-27 15:50:03 -0700 | [diff] [blame] | 1270 | rc = -ENOMEM; |
| 1271 | goto user_error; |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 1272 | } |
| 1273 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1274 | mutex_init(&dbgdev->dbg_mutex); |
| 1275 | |
| 1276 | dbgdev->dev = dev; |
| 1277 | |
| 1278 | dent = debugfs_create_dir("pm8xxx-pwm-dbg", NULL); |
| 1279 | if (dent == NULL || IS_ERR(dent)) { |
| 1280 | pr_err("ERR debugfs_create_dir: dent=%p\n", dent); |
Jay Chokshi | 00c07cc | 2011-10-27 15:50:03 -0700 | [diff] [blame] | 1281 | rc = -ENOMEM; |
| 1282 | goto dir_error; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | dbgdev->dent = dent; |
| 1286 | |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 1287 | for (i = 0; i < pwm_chip->pwm_channels; i++) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1288 | char pwm_ch[] = "0"; |
| 1289 | |
| 1290 | pwm_ch[0] = '0' + i; |
| 1291 | dent = debugfs_create_dir(pwm_ch, dbgdev->dent); |
| 1292 | if (dent == NULL || IS_ERR(dent)) { |
| 1293 | pr_err("ERR: pwm=%d: dir: dent=%p\n", i, dent); |
Jay Chokshi | 00c07cc | 2011-10-27 15:50:03 -0700 | [diff] [blame] | 1294 | rc = -ENOMEM; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1295 | goto debug_error; |
| 1296 | } |
| 1297 | |
| 1298 | puser = &dbgdev->user[i]; |
| 1299 | puser->dbgdev = dbgdev; |
| 1300 | puser->pwm_id = i; |
| 1301 | temp = debugfs_create_file("period", S_IRUGO | S_IWUSR, |
| 1302 | dent, puser, &dbg_pwm_period_fops); |
| 1303 | if (temp == NULL || IS_ERR(temp)) { |
| 1304 | pr_err("ERR: pwm=%d: period: dent=%p\n", i, dent); |
Jay Chokshi | 00c07cc | 2011-10-27 15:50:03 -0700 | [diff] [blame] | 1305 | rc = -ENOMEM; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1306 | goto debug_error; |
| 1307 | } |
| 1308 | |
| 1309 | temp = debugfs_create_file("duty-cycle", S_IRUGO | S_IWUSR, |
| 1310 | dent, puser, &dbg_pwm_duty_cycle_fops); |
| 1311 | if (temp == NULL || IS_ERR(temp)) { |
| 1312 | pr_err("ERR: pwm=%d: duty-cycle: dent=%p\n", i, dent); |
Jay Chokshi | 00c07cc | 2011-10-27 15:50:03 -0700 | [diff] [blame] | 1313 | rc = -ENOMEM; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1314 | goto debug_error; |
| 1315 | } |
| 1316 | |
| 1317 | temp = debugfs_create_file("enable", S_IRUGO | S_IWUSR, |
| 1318 | dent, puser, &dbg_pwm_enable_fops); |
| 1319 | if (temp == NULL || IS_ERR(temp)) { |
| 1320 | pr_err("ERR: pwm=%d: enable: dent=%p\n", i, dent); |
Jay Chokshi | 00c07cc | 2011-10-27 15:50:03 -0700 | [diff] [blame] | 1321 | rc = -ENOMEM; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1322 | goto debug_error; |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | pmic_dbg_device = dbgdev; |
| 1327 | |
| 1328 | return 0; |
| 1329 | |
| 1330 | debug_error: |
| 1331 | debugfs_remove_recursive(dbgdev->dent); |
Jay Chokshi | 00c07cc | 2011-10-27 15:50:03 -0700 | [diff] [blame] | 1332 | dir_error: |
| 1333 | kfree(dbgdev->user); |
| 1334 | user_error: |
| 1335 | kfree(dbgdev); |
| 1336 | return rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | static int __devexit pm8xxx_pwm_dbg_remove(void) |
| 1340 | { |
| 1341 | if (pmic_dbg_device) { |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 1342 | kfree(pmic_dbg_device->user); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1343 | debugfs_remove_recursive(pmic_dbg_device->dent); |
| 1344 | kfree(pmic_dbg_device); |
| 1345 | } |
| 1346 | return 0; |
| 1347 | } |
| 1348 | |
| 1349 | #else |
| 1350 | |
| 1351 | static int __devinit pm8xxx_pwm_dbg_probe(struct device *dev) |
| 1352 | { |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
| 1356 | static int __devexit pm8xxx_pwm_dbg_remove(void) |
| 1357 | { |
| 1358 | return 0; |
| 1359 | } |
| 1360 | |
| 1361 | #endif |
| 1362 | |
| 1363 | static int __devinit pm8xxx_pwm_probe(struct platform_device *pdev) |
| 1364 | { |
Jay Chokshi | b0a0fa5 | 2012-02-23 16:18:44 -0800 | [diff] [blame^] | 1365 | const struct pm8xxx_pwm_platform_data *pdata = pdev->dev.platform_data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1366 | struct pm8xxx_pwm_chip *chip; |
| 1367 | int i; |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 1368 | enum pm8xxx_version version; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1369 | |
| 1370 | chip = kzalloc(sizeof *chip, GFP_KERNEL); |
| 1371 | if (chip == NULL) { |
| 1372 | pr_err("kzalloc() failed.\n"); |
| 1373 | return -ENOMEM; |
| 1374 | } |
| 1375 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1376 | mutex_init(&chip->pwm_mutex); |
| 1377 | |
| 1378 | chip->dev = &pdev->dev; |
| 1379 | pwm_chip = chip; |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 1380 | |
| 1381 | version = pm8xxx_get_version(chip->dev->parent); |
| 1382 | |
| 1383 | if (version == PM8XXX_VERSION_8921 || |
David Collins | 999480d | 2011-11-16 08:52:30 -0800 | [diff] [blame] | 1384 | version == PM8XXX_VERSION_8058 || |
Jay Chokshi | 0c220cd | 2011-12-09 17:18:20 -0800 | [diff] [blame] | 1385 | version == PM8XXX_VERSION_8922 || |
| 1386 | version == PM8XXX_VERSION_8038) { |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 1387 | chip->is_lpg_supported = 1; |
| 1388 | } |
| 1389 | if (chip->is_lpg_supported) { |
Jay Chokshi | 0c220cd | 2011-12-09 17:18:20 -0800 | [diff] [blame] | 1390 | if (version == PM8XXX_VERSION_8922 || |
| 1391 | version == PM8XXX_VERSION_8038) { |
| 1392 | for (i = 0; i < NUM_CLOCKS; i++) |
| 1393 | pt_t[0][i] /= PRE_DIVIDE_2; |
| 1394 | chip->pwm_channels = PM8XXX_LPG_V1_PWM_CHANNELS; |
| 1395 | } else { |
| 1396 | chip->pwm_channels = PM8XXX_LPG_V0_PWM_CHANNELS; |
| 1397 | } |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 1398 | chip->pwm_total_pre_divs = NUM_LPG_PRE_DIVIDE; |
| 1399 | } else { |
| 1400 | chip->pwm_channels = PM8XXX_PWM_CHANNELS; |
| 1401 | chip->pwm_total_pre_divs = NUM_PWM_PRE_DIVIDE; |
| 1402 | } |
| 1403 | |
| 1404 | chip->pwm_dev = kcalloc(chip->pwm_channels, sizeof(struct pwm_device), |
| 1405 | GFP_KERNEL); |
| 1406 | if (chip->pwm_dev == NULL) { |
| 1407 | pr_err("kcalloc() failed.\n"); |
| 1408 | mutex_destroy(&chip->pwm_mutex); |
| 1409 | kfree(chip); |
| 1410 | return -ENOMEM; |
| 1411 | } |
| 1412 | |
| 1413 | for (i = 0; i < chip->pwm_channels; i++) { |
| 1414 | chip->pwm_dev[i].pwm_id = i; |
| 1415 | chip->pwm_dev[i].chip = chip; |
Jay Chokshi | b0a0fa5 | 2012-02-23 16:18:44 -0800 | [diff] [blame^] | 1416 | if (i == pdata->dtest_channel) |
| 1417 | chip->pwm_dev[i].dtest_mode_supported = 1; |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1420 | platform_set_drvdata(pdev, chip); |
| 1421 | |
| 1422 | if (pm8xxx_pwm_dbg_probe(&pdev->dev) < 0) |
| 1423 | pr_err("could not set up debugfs\n"); |
| 1424 | |
| 1425 | pr_notice("OK\n"); |
| 1426 | return 0; |
| 1427 | } |
| 1428 | |
| 1429 | static int __devexit pm8xxx_pwm_remove(struct platform_device *pdev) |
| 1430 | { |
| 1431 | struct pm8xxx_pwm_chip *chip = dev_get_drvdata(pdev->dev.parent); |
| 1432 | |
| 1433 | pm8xxx_pwm_dbg_remove(); |
Jay Chokshi | 5765686 | 2011-09-07 12:02:22 -0700 | [diff] [blame] | 1434 | kfree(chip->pwm_dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1435 | mutex_destroy(&chip->pwm_mutex); |
| 1436 | platform_set_drvdata(pdev, NULL); |
| 1437 | kfree(chip); |
| 1438 | return 0; |
| 1439 | } |
| 1440 | |
| 1441 | static struct platform_driver pm8xxx_pwm_driver = { |
| 1442 | .probe = pm8xxx_pwm_probe, |
| 1443 | .remove = __devexit_p(pm8xxx_pwm_remove), |
| 1444 | .driver = { |
| 1445 | .name = PM8XXX_PWM_DEV_NAME, |
| 1446 | .owner = THIS_MODULE, |
| 1447 | }, |
| 1448 | }; |
| 1449 | |
| 1450 | static int __init pm8xxx_pwm_init(void) |
| 1451 | { |
| 1452 | return platform_driver_register(&pm8xxx_pwm_driver); |
| 1453 | } |
| 1454 | |
| 1455 | static void __exit pm8xxx_pwm_exit(void) |
| 1456 | { |
| 1457 | platform_driver_unregister(&pm8xxx_pwm_driver); |
| 1458 | } |
| 1459 | |
| 1460 | subsys_initcall(pm8xxx_pwm_init); |
| 1461 | module_exit(pm8xxx_pwm_exit); |
| 1462 | |
| 1463 | MODULE_LICENSE("GPL v2"); |
| 1464 | MODULE_DESCRIPTION("PM8XXX PWM driver"); |
| 1465 | MODULE_VERSION("1.0"); |
| 1466 | MODULE_ALIAS("platform:" PM8XXX_PWM_DEV_NAME); |