Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2010-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 | |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 13 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 14 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 15 | #include <linux/kernel.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 16 | #include <linux/module.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 17 | #include <linux/init.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/leds.h> |
| 21 | #include <linux/workqueue.h> |
Jay Chokshi | 868312e | 2011-09-16 13:57:13 -0700 | [diff] [blame] | 22 | #include <linux/err.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 23 | |
| 24 | #include <linux/mfd/pm8xxx/core.h> |
Jay Chokshi | 868312e | 2011-09-16 13:57:13 -0700 | [diff] [blame] | 25 | #include <linux/mfd/pm8xxx/pwm.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 26 | #include <linux/leds-pm8xxx.h> |
| 27 | |
| 28 | #define SSBI_REG_ADDR_DRV_KEYPAD 0x48 |
| 29 | #define PM8XXX_DRV_KEYPAD_BL_MASK 0xf0 |
| 30 | #define PM8XXX_DRV_KEYPAD_BL_SHIFT 0x04 |
| 31 | |
| 32 | #define SSBI_REG_ADDR_FLASH_DRV0 0x49 |
| 33 | #define PM8XXX_DRV_FLASH_MASK 0xf0 |
| 34 | #define PM8XXX_DRV_FLASH_SHIFT 0x04 |
| 35 | |
| 36 | #define SSBI_REG_ADDR_FLASH_DRV1 0xFB |
| 37 | |
| 38 | #define SSBI_REG_ADDR_LED_CTRL_BASE 0x131 |
| 39 | #define SSBI_REG_ADDR_LED_CTRL(n) (SSBI_REG_ADDR_LED_CTRL_BASE + (n)) |
| 40 | #define PM8XXX_DRV_LED_CTRL_MASK 0xf8 |
| 41 | #define PM8XXX_DRV_LED_CTRL_SHIFT 0x03 |
| 42 | |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 43 | #define SSBI_REG_ADDR_WLED_CTRL_BASE 0x25A |
| 44 | #define SSBI_REG_ADDR_WLED_CTRL(n) (SSBI_REG_ADDR_WLED_CTRL_BASE + (n) - 1) |
| 45 | |
| 46 | /* wled control registers */ |
| 47 | #define WLED_MOD_CTRL_REG SSBI_REG_ADDR_WLED_CTRL(1) |
| 48 | #define WLED_MAX_CURR_CFG_REG(n) SSBI_REG_ADDR_WLED_CTRL(n + 2) |
Mohan Pallaka | 58ff405 | 2012-11-19 16:58:59 +0530 | [diff] [blame] | 49 | #define WLED_BRIGHTNESS_CNTL_REG1(n) SSBI_REG_ADDR_WLED_CTRL((2 * n) + 5) |
| 50 | #define WLED_BRIGHTNESS_CNTL_REG2(n) SSBI_REG_ADDR_WLED_CTRL((2 * n) + 6) |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 51 | #define WLED_SYNC_REG SSBI_REG_ADDR_WLED_CTRL(11) |
| 52 | #define WLED_OVP_CFG_REG SSBI_REG_ADDR_WLED_CTRL(13) |
| 53 | #define WLED_BOOST_CFG_REG SSBI_REG_ADDR_WLED_CTRL(14) |
| 54 | #define WLED_HIGH_POLE_CAP_REG SSBI_REG_ADDR_WLED_CTRL(16) |
| 55 | |
| 56 | #define WLED_STRINGS 0x03 |
| 57 | #define WLED_OVP_VAL_MASK 0x30 |
| 58 | #define WLED_OVP_VAL_BIT_SHFT 0x04 |
| 59 | #define WLED_BOOST_LIMIT_MASK 0xE0 |
| 60 | #define WLED_BOOST_LIMIT_BIT_SHFT 0x05 |
Amy Maloche | 56913f5 | 2012-05-11 10:47:24 -0700 | [diff] [blame] | 61 | #define WLED_BOOST_OFF 0x00 |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 62 | #define WLED_EN_MASK 0x01 |
| 63 | #define WLED_CP_SELECT_MAX 0x03 |
| 64 | #define WLED_CP_SELECT_MASK 0x03 |
| 65 | #define WLED_DIG_MOD_GEN_MASK 0x70 |
| 66 | #define WLED_CS_OUT_MASK 0x0E |
| 67 | #define WLED_CTL_DLY_STEP 200 |
| 68 | #define WLED_CTL_DLY_MAX 1400 |
| 69 | #define WLED_CTL_DLY_MASK 0xE0 |
| 70 | #define WLED_CTL_DLY_BIT_SHFT 0x05 |
| 71 | #define WLED_MAX_CURR 25 |
| 72 | #define WLED_MAX_CURR_MASK 0x1F |
Amy Maloche | be73e19 | 2012-11-29 12:53:59 -0800 | [diff] [blame^] | 73 | #define WLED_BRIGHTNESS_MSB_MASK 0x0F |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 74 | #define WLED_OP_FDBCK_MASK 0x1C |
| 75 | #define WLED_OP_FDBCK_BIT_SHFT 0x02 |
| 76 | |
Chandan Uddaraju | 6e73f0a | 2012-03-08 17:32:55 -0800 | [diff] [blame] | 77 | #define WLED_MAX_LEVEL 255 |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 78 | #define WLED_8_BIT_MASK 0xFF |
| 79 | #define WLED_8_BIT_SHFT 0x08 |
| 80 | #define WLED_MAX_DUTY_CYCLE 0xFFF |
| 81 | |
| 82 | #define WLED_SYNC_VAL 0x07 |
| 83 | #define WLED_SYNC_RESET_VAL 0x00 |
Amy Maloche | c366d5b | 2012-08-28 11:06:40 -0700 | [diff] [blame] | 84 | #define WLED_SYNC_MASK 0xF8 |
| 85 | |
| 86 | #define ONE_WLED_STRING 1 |
| 87 | #define TWO_WLED_STRINGS 2 |
| 88 | #define THREE_WLED_STRINGS 3 |
| 89 | |
| 90 | #define WLED_CABC_ONE_STRING 0x01 |
| 91 | #define WLED_CABC_TWO_STRING 0x03 |
| 92 | #define WLED_CABC_THREE_STRING 0x07 |
| 93 | |
| 94 | #define WLED_CABC_SHIFT 3 |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 95 | |
Amy Maloche | c17c373 | 2012-02-27 18:34:07 -0800 | [diff] [blame] | 96 | #define SSBI_REG_ADDR_RGB_CNTL1 0x12D |
| 97 | #define SSBI_REG_ADDR_RGB_CNTL2 0x12E |
| 98 | |
| 99 | #define PM8XXX_DRV_RGB_RED_LED BIT(2) |
| 100 | #define PM8XXX_DRV_RGB_GREEN_LED BIT(1) |
| 101 | #define PM8XXX_DRV_RGB_BLUE_LED BIT(0) |
| 102 | |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 103 | #define MAX_FLASH_LED_CURRENT 300 |
| 104 | #define MAX_LC_LED_CURRENT 40 |
| 105 | #define MAX_KP_BL_LED_CURRENT 300 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 106 | |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 107 | #define PM8XXX_ID_LED_CURRENT_FACTOR 2 /* Iout = x * 2mA */ |
| 108 | #define PM8XXX_ID_FLASH_CURRENT_FACTOR 20 /* Iout = x * 20mA */ |
| 109 | |
| 110 | #define PM8XXX_FLASH_MODE_DBUS1 1 |
| 111 | #define PM8XXX_FLASH_MODE_DBUS2 2 |
| 112 | #define PM8XXX_FLASH_MODE_PWM 3 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 113 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 114 | #define MAX_LC_LED_BRIGHTNESS 20 |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 115 | #define MAX_FLASH_BRIGHTNESS 15 |
| 116 | #define MAX_KB_LED_BRIGHTNESS 15 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 117 | |
Jay Chokshi | de4cefb | 2011-08-04 18:10:44 -0700 | [diff] [blame] | 118 | #define PM8XXX_LED_OFFSET(id) ((id) - PM8XXX_ID_LED_0) |
| 119 | |
Jay Chokshi | 868312e | 2011-09-16 13:57:13 -0700 | [diff] [blame] | 120 | #define PM8XXX_LED_PWM_FLAGS (PM_PWM_LUT_LOOP | PM_PWM_LUT_RAMP_UP) |
| 121 | |
Amy Maloche | 3d2c24c | 2012-03-22 19:02:25 -0700 | [diff] [blame] | 122 | #define LED_MAP(_version, _kb, _led0, _led1, _led2, _flash_led0, _flash_led1, \ |
Amy Maloche | c17c373 | 2012-02-27 18:34:07 -0800 | [diff] [blame] | 123 | _wled, _rgb_led_red, _rgb_led_green, _rgb_led_blue)\ |
Amy Maloche | 3d2c24c | 2012-03-22 19:02:25 -0700 | [diff] [blame] | 124 | {\ |
| 125 | .version = _version,\ |
| 126 | .supported = _kb << PM8XXX_ID_LED_KB_LIGHT | \ |
| 127 | _led0 << PM8XXX_ID_LED_0 | _led1 << PM8XXX_ID_LED_1 | \ |
| 128 | _led2 << PM8XXX_ID_LED_2 | \ |
| 129 | _flash_led0 << PM8XXX_ID_FLASH_LED_0 | \ |
| 130 | _flash_led1 << PM8XXX_ID_FLASH_LED_1 | \ |
Amy Maloche | c17c373 | 2012-02-27 18:34:07 -0800 | [diff] [blame] | 131 | _wled << PM8XXX_ID_WLED | \ |
| 132 | _rgb_led_red << PM8XXX_ID_RGB_LED_RED | \ |
| 133 | _rgb_led_green << PM8XXX_ID_RGB_LED_GREEN | \ |
| 134 | _rgb_led_blue << PM8XXX_ID_RGB_LED_BLUE, \ |
Amy Maloche | 3d2c24c | 2012-03-22 19:02:25 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /** |
| 138 | * supported_leds - leds supported for each PMIC version |
| 139 | * @version - version of PMIC |
| 140 | * @supported - which leds are supported on version |
| 141 | */ |
| 142 | |
| 143 | struct supported_leds { |
| 144 | enum pm8xxx_version version; |
| 145 | u32 supported; |
| 146 | }; |
| 147 | |
| 148 | static const struct supported_leds led_map[] = { |
Amy Maloche | c17c373 | 2012-02-27 18:34:07 -0800 | [diff] [blame] | 149 | LED_MAP(PM8XXX_VERSION_8058, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0), |
| 150 | LED_MAP(PM8XXX_VERSION_8921, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0), |
| 151 | LED_MAP(PM8XXX_VERSION_8018, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0), |
| 152 | LED_MAP(PM8XXX_VERSION_8922, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1), |
| 153 | LED_MAP(PM8XXX_VERSION_8038, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1), |
Amy Maloche | 3d2c24c | 2012-03-22 19:02:25 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 156 | /** |
| 157 | * struct pm8xxx_led_data - internal led data structure |
| 158 | * @led_classdev - led class device |
| 159 | * @id - led index |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 160 | * @work - workqueue for led |
| 161 | * @lock - to protect the transactions |
| 162 | * @reg - cached value of led register |
Jay Chokshi | 868312e | 2011-09-16 13:57:13 -0700 | [diff] [blame] | 163 | * @pwm_dev - pointer to PWM device if LED is driven using PWM |
| 164 | * @pwm_channel - PWM channel ID |
| 165 | * @pwm_period_us - PWM period in micro seconds |
| 166 | * @pwm_duty_cycles - struct that describes PWM duty cycles info |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 167 | */ |
| 168 | struct pm8xxx_led_data { |
| 169 | struct led_classdev cdev; |
| 170 | int id; |
| 171 | u8 reg; |
Amy Maloche | 56913f5 | 2012-05-11 10:47:24 -0700 | [diff] [blame] | 172 | u8 wled_mod_ctrl_val; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 173 | struct device *dev; |
| 174 | struct work_struct work; |
| 175 | struct mutex lock; |
Jay Chokshi | 868312e | 2011-09-16 13:57:13 -0700 | [diff] [blame] | 176 | struct pwm_device *pwm_dev; |
| 177 | int pwm_channel; |
| 178 | u32 pwm_period_us; |
| 179 | struct pm8xxx_pwm_duty_cycles *pwm_duty_cycles; |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 180 | struct wled_config_data *wled_cfg; |
| 181 | int max_current; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | static void led_kp_set(struct pm8xxx_led_data *led, enum led_brightness value) |
| 185 | { |
| 186 | int rc; |
| 187 | u8 level; |
| 188 | |
| 189 | level = (value << PM8XXX_DRV_KEYPAD_BL_SHIFT) & |
| 190 | PM8XXX_DRV_KEYPAD_BL_MASK; |
| 191 | |
| 192 | led->reg &= ~PM8XXX_DRV_KEYPAD_BL_MASK; |
| 193 | led->reg |= level; |
| 194 | |
| 195 | rc = pm8xxx_writeb(led->dev->parent, SSBI_REG_ADDR_DRV_KEYPAD, |
| 196 | led->reg); |
| 197 | if (rc < 0) |
| 198 | dev_err(led->cdev.dev, |
| 199 | "can't set keypad backlight level rc=%d\n", rc); |
| 200 | } |
| 201 | |
| 202 | static void led_lc_set(struct pm8xxx_led_data *led, enum led_brightness value) |
| 203 | { |
| 204 | int rc, offset; |
| 205 | u8 level; |
| 206 | |
| 207 | level = (value << PM8XXX_DRV_LED_CTRL_SHIFT) & |
| 208 | PM8XXX_DRV_LED_CTRL_MASK; |
| 209 | |
| 210 | offset = PM8XXX_LED_OFFSET(led->id); |
| 211 | |
| 212 | led->reg &= ~PM8XXX_DRV_LED_CTRL_MASK; |
| 213 | led->reg |= level; |
| 214 | |
| 215 | rc = pm8xxx_writeb(led->dev->parent, SSBI_REG_ADDR_LED_CTRL(offset), |
| 216 | led->reg); |
| 217 | if (rc) |
| 218 | dev_err(led->cdev.dev, "can't set (%d) led value rc=%d\n", |
| 219 | led->id, rc); |
| 220 | } |
| 221 | |
| 222 | static void |
| 223 | led_flash_set(struct pm8xxx_led_data *led, enum led_brightness value) |
| 224 | { |
| 225 | int rc; |
| 226 | u8 level; |
| 227 | u16 reg_addr; |
| 228 | |
| 229 | level = (value << PM8XXX_DRV_FLASH_SHIFT) & |
| 230 | PM8XXX_DRV_FLASH_MASK; |
| 231 | |
| 232 | led->reg &= ~PM8XXX_DRV_FLASH_MASK; |
| 233 | led->reg |= level; |
| 234 | |
| 235 | if (led->id == PM8XXX_ID_FLASH_LED_0) |
| 236 | reg_addr = SSBI_REG_ADDR_FLASH_DRV0; |
| 237 | else |
| 238 | reg_addr = SSBI_REG_ADDR_FLASH_DRV1; |
| 239 | |
| 240 | rc = pm8xxx_writeb(led->dev->parent, reg_addr, led->reg); |
| 241 | if (rc < 0) |
| 242 | dev_err(led->cdev.dev, "can't set flash led%d level rc=%d\n", |
| 243 | led->id, rc); |
| 244 | } |
| 245 | |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 246 | static int |
| 247 | led_wled_set(struct pm8xxx_led_data *led, enum led_brightness value) |
| 248 | { |
| 249 | int rc, duty; |
| 250 | u8 val, i, num_wled_strings; |
| 251 | |
| 252 | if (value > WLED_MAX_LEVEL) |
| 253 | value = WLED_MAX_LEVEL; |
| 254 | |
Amy Maloche | 56913f5 | 2012-05-11 10:47:24 -0700 | [diff] [blame] | 255 | if (value == 0) { |
| 256 | rc = pm8xxx_writeb(led->dev->parent, WLED_MOD_CTRL_REG, |
| 257 | WLED_BOOST_OFF); |
| 258 | if (rc) { |
| 259 | dev_err(led->dev->parent, "can't write wled ctrl config" |
| 260 | " register rc=%d\n", rc); |
| 261 | return rc; |
| 262 | } |
| 263 | } else { |
| 264 | rc = pm8xxx_writeb(led->dev->parent, WLED_MOD_CTRL_REG, |
| 265 | led->wled_mod_ctrl_val); |
| 266 | if (rc) { |
| 267 | dev_err(led->dev->parent, "can't write wled ctrl config" |
| 268 | " register rc=%d\n", rc); |
| 269 | return rc; |
| 270 | } |
| 271 | } |
| 272 | |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 273 | duty = (WLED_MAX_DUTY_CYCLE * value) / WLED_MAX_LEVEL; |
| 274 | |
| 275 | num_wled_strings = led->wled_cfg->num_strings; |
| 276 | |
| 277 | /* program brightness control registers */ |
| 278 | for (i = 0; i < num_wled_strings; i++) { |
| 279 | rc = pm8xxx_readb(led->dev->parent, |
| 280 | WLED_BRIGHTNESS_CNTL_REG1(i), &val); |
| 281 | if (rc) { |
| 282 | dev_err(led->dev->parent, "can't read wled brightnes ctrl" |
| 283 | " register1 rc=%d\n", rc); |
| 284 | return rc; |
| 285 | } |
| 286 | |
Amy Maloche | be73e19 | 2012-11-29 12:53:59 -0800 | [diff] [blame^] | 287 | val = (val & ~WLED_BRIGHTNESS_MSB_MASK) | |
| 288 | (duty >> WLED_8_BIT_SHFT); |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 289 | rc = pm8xxx_writeb(led->dev->parent, |
| 290 | WLED_BRIGHTNESS_CNTL_REG1(i), val); |
| 291 | if (rc) { |
| 292 | dev_err(led->dev->parent, "can't write wled brightness ctrl" |
| 293 | " register1 rc=%d\n", rc); |
| 294 | return rc; |
| 295 | } |
| 296 | |
| 297 | val = duty & WLED_8_BIT_MASK; |
| 298 | rc = pm8xxx_writeb(led->dev->parent, |
| 299 | WLED_BRIGHTNESS_CNTL_REG2(i), val); |
| 300 | if (rc) { |
| 301 | dev_err(led->dev->parent, "can't write wled brightness ctrl" |
| 302 | " register2 rc=%d\n", rc); |
| 303 | return rc; |
| 304 | } |
| 305 | } |
Amy Maloche | c366d5b | 2012-08-28 11:06:40 -0700 | [diff] [blame] | 306 | rc = pm8xxx_readb(led->dev->parent, WLED_SYNC_REG, &val); |
| 307 | if (rc) { |
| 308 | dev_err(led->dev->parent, |
| 309 | "can't read wled sync register rc=%d\n", rc); |
| 310 | return rc; |
| 311 | } |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 312 | /* sync */ |
Amy Maloche | c366d5b | 2012-08-28 11:06:40 -0700 | [diff] [blame] | 313 | val &= WLED_SYNC_MASK; |
| 314 | val |= WLED_SYNC_VAL; |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 315 | rc = pm8xxx_writeb(led->dev->parent, WLED_SYNC_REG, val); |
| 316 | if (rc) { |
| 317 | dev_err(led->dev->parent, |
| 318 | "can't read wled sync register rc=%d\n", rc); |
| 319 | return rc; |
| 320 | } |
Amy Maloche | c366d5b | 2012-08-28 11:06:40 -0700 | [diff] [blame] | 321 | val &= WLED_SYNC_MASK; |
| 322 | val |= WLED_SYNC_RESET_VAL; |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 323 | rc = pm8xxx_writeb(led->dev->parent, WLED_SYNC_REG, val); |
| 324 | if (rc) { |
| 325 | dev_err(led->dev->parent, |
| 326 | "can't read wled sync register rc=%d\n", rc); |
| 327 | return rc; |
| 328 | } |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static void wled_dump_regs(struct pm8xxx_led_data *led) |
| 333 | { |
| 334 | int i; |
| 335 | u8 val; |
| 336 | |
| 337 | for (i = 1; i < 17; i++) { |
| 338 | pm8xxx_readb(led->dev->parent, |
| 339 | SSBI_REG_ADDR_WLED_CTRL(i), &val); |
| 340 | pr_debug("WLED_CTRL_%d = 0x%x\n", i, val); |
| 341 | } |
| 342 | } |
| 343 | |
Amy Maloche | c17c373 | 2012-02-27 18:34:07 -0800 | [diff] [blame] | 344 | static void |
Amy Maloche | ad3c784 | 2012-07-20 14:51:27 -0700 | [diff] [blame] | 345 | led_rgb_write(struct pm8xxx_led_data *led, u16 addr, enum led_brightness value) |
Amy Maloche | c17c373 | 2012-02-27 18:34:07 -0800 | [diff] [blame] | 346 | { |
| 347 | int rc; |
| 348 | u8 val, mask; |
| 349 | |
Amy Maloche | ad3c784 | 2012-07-20 14:51:27 -0700 | [diff] [blame] | 350 | if (led->id != PM8XXX_ID_RGB_LED_BLUE && |
| 351 | led->id != PM8XXX_ID_RGB_LED_RED && |
| 352 | led->id != PM8XXX_ID_RGB_LED_GREEN) |
| 353 | return; |
| 354 | |
| 355 | rc = pm8xxx_readb(led->dev->parent, addr, &val); |
Amy Maloche | c17c373 | 2012-02-27 18:34:07 -0800 | [diff] [blame] | 356 | if (rc) { |
| 357 | dev_err(led->cdev.dev, "can't read rgb ctrl register rc=%d\n", |
| 358 | rc); |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | switch (led->id) { |
| 363 | case PM8XXX_ID_RGB_LED_RED: |
| 364 | mask = PM8XXX_DRV_RGB_RED_LED; |
| 365 | break; |
| 366 | case PM8XXX_ID_RGB_LED_GREEN: |
| 367 | mask = PM8XXX_DRV_RGB_GREEN_LED; |
| 368 | break; |
| 369 | case PM8XXX_ID_RGB_LED_BLUE: |
| 370 | mask = PM8XXX_DRV_RGB_BLUE_LED; |
| 371 | break; |
| 372 | default: |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | if (value) |
| 377 | val |= mask; |
| 378 | else |
| 379 | val &= ~mask; |
| 380 | |
Amy Maloche | ad3c784 | 2012-07-20 14:51:27 -0700 | [diff] [blame] | 381 | rc = pm8xxx_writeb(led->dev->parent, addr, val); |
Amy Maloche | c17c373 | 2012-02-27 18:34:07 -0800 | [diff] [blame] | 382 | if (rc < 0) |
| 383 | dev_err(led->cdev.dev, "can't set rgb led %d level rc=%d\n", |
| 384 | led->id, rc); |
| 385 | } |
| 386 | |
Amy Maloche | ad3c784 | 2012-07-20 14:51:27 -0700 | [diff] [blame] | 387 | static void |
| 388 | led_rgb_set(struct pm8xxx_led_data *led, enum led_brightness value) |
| 389 | { |
| 390 | if (value) { |
| 391 | led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL1, value); |
| 392 | led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL2, value); |
| 393 | } else { |
| 394 | led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL2, value); |
| 395 | led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL1, value); |
| 396 | } |
| 397 | } |
| 398 | |
Jay Chokshi | 868312e | 2011-09-16 13:57:13 -0700 | [diff] [blame] | 399 | static int pm8xxx_led_pwm_work(struct pm8xxx_led_data *led) |
| 400 | { |
| 401 | int duty_us; |
| 402 | int rc = 0; |
| 403 | |
| 404 | if (led->pwm_duty_cycles == NULL) { |
| 405 | duty_us = (led->pwm_period_us * led->cdev.brightness) / |
| 406 | LED_FULL; |
| 407 | rc = pwm_config(led->pwm_dev, duty_us, led->pwm_period_us); |
Amy Maloche | ad3c784 | 2012-07-20 14:51:27 -0700 | [diff] [blame] | 408 | if (led->cdev.brightness) { |
| 409 | led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL1, |
| 410 | led->cdev.brightness); |
Jay Chokshi | 868312e | 2011-09-16 13:57:13 -0700 | [diff] [blame] | 411 | rc = pwm_enable(led->pwm_dev); |
Amy Maloche | ad3c784 | 2012-07-20 14:51:27 -0700 | [diff] [blame] | 412 | } else { |
Jay Chokshi | 868312e | 2011-09-16 13:57:13 -0700 | [diff] [blame] | 413 | pwm_disable(led->pwm_dev); |
Amy Maloche | ad3c784 | 2012-07-20 14:51:27 -0700 | [diff] [blame] | 414 | led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL1, |
| 415 | led->cdev.brightness); |
| 416 | } |
Jay Chokshi | 868312e | 2011-09-16 13:57:13 -0700 | [diff] [blame] | 417 | } else { |
Amy Maloche | ad3c784 | 2012-07-20 14:51:27 -0700 | [diff] [blame] | 418 | if (led->cdev.brightness) |
| 419 | led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL1, |
| 420 | led->cdev.brightness); |
Jay Chokshi | 868312e | 2011-09-16 13:57:13 -0700 | [diff] [blame] | 421 | rc = pm8xxx_pwm_lut_enable(led->pwm_dev, led->cdev.brightness); |
Amy Maloche | ad3c784 | 2012-07-20 14:51:27 -0700 | [diff] [blame] | 422 | if (!led->cdev.brightness) |
| 423 | led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL1, |
| 424 | led->cdev.brightness); |
Jay Chokshi | 868312e | 2011-09-16 13:57:13 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | return rc; |
| 428 | } |
| 429 | |
Jay Chokshi | de4cefb | 2011-08-04 18:10:44 -0700 | [diff] [blame] | 430 | static void __pm8xxx_led_work(struct pm8xxx_led_data *led, |
| 431 | enum led_brightness level) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 432 | { |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 433 | int rc; |
| 434 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 435 | mutex_lock(&led->lock); |
| 436 | |
| 437 | switch (led->id) { |
| 438 | case PM8XXX_ID_LED_KB_LIGHT: |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 439 | led_kp_set(led, level); |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 440 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 441 | case PM8XXX_ID_LED_0: |
| 442 | case PM8XXX_ID_LED_1: |
| 443 | case PM8XXX_ID_LED_2: |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 444 | led_lc_set(led, level); |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 445 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 446 | case PM8XXX_ID_FLASH_LED_0: |
| 447 | case PM8XXX_ID_FLASH_LED_1: |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 448 | led_flash_set(led, level); |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 449 | break; |
| 450 | case PM8XXX_ID_WLED: |
| 451 | rc = led_wled_set(led, level); |
| 452 | if (rc < 0) |
| 453 | pr_err("wled brightness set failed %d\n", rc); |
| 454 | break; |
Amy Maloche | c17c373 | 2012-02-27 18:34:07 -0800 | [diff] [blame] | 455 | case PM8XXX_ID_RGB_LED_RED: |
| 456 | case PM8XXX_ID_RGB_LED_GREEN: |
| 457 | case PM8XXX_ID_RGB_LED_BLUE: |
| 458 | led_rgb_set(led, level); |
| 459 | break; |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 460 | default: |
| 461 | dev_err(led->cdev.dev, "unknown led id %d", led->id); |
| 462 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | mutex_unlock(&led->lock); |
| 466 | } |
| 467 | |
Jay Chokshi | de4cefb | 2011-08-04 18:10:44 -0700 | [diff] [blame] | 468 | static void pm8xxx_led_work(struct work_struct *work) |
| 469 | { |
Jay Chokshi | 868312e | 2011-09-16 13:57:13 -0700 | [diff] [blame] | 470 | int rc; |
| 471 | |
Jay Chokshi | de4cefb | 2011-08-04 18:10:44 -0700 | [diff] [blame] | 472 | struct pm8xxx_led_data *led = container_of(work, |
| 473 | struct pm8xxx_led_data, work); |
Jay Chokshi | de4cefb | 2011-08-04 18:10:44 -0700 | [diff] [blame] | 474 | |
Jay Chokshi | 868312e | 2011-09-16 13:57:13 -0700 | [diff] [blame] | 475 | if (led->pwm_dev == NULL) { |
| 476 | __pm8xxx_led_work(led, led->cdev.brightness); |
| 477 | } else { |
| 478 | rc = pm8xxx_led_pwm_work(led); |
| 479 | if (rc) |
| 480 | pr_err("could not configure PWM mode for LED:%d\n", |
| 481 | led->id); |
| 482 | } |
Jay Chokshi | de4cefb | 2011-08-04 18:10:44 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 485 | static void pm8xxx_led_set(struct led_classdev *led_cdev, |
| 486 | enum led_brightness value) |
| 487 | { |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 488 | struct pm8xxx_led_data *led; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 489 | |
| 490 | led = container_of(led_cdev, struct pm8xxx_led_data, cdev); |
| 491 | |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 492 | if (value < LED_OFF || value > led->cdev.max_brightness) { |
| 493 | dev_err(led->cdev.dev, "Invalid brightness value exceeds"); |
| 494 | return; |
| 495 | } |
| 496 | |
| 497 | led->cdev.brightness = value; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 498 | schedule_work(&led->work); |
| 499 | } |
| 500 | |
Jay Chokshi | de4cefb | 2011-08-04 18:10:44 -0700 | [diff] [blame] | 501 | static int pm8xxx_set_led_mode_and_max_brightness(struct pm8xxx_led_data *led, |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 502 | enum pm8xxx_led_modes led_mode, int max_current) |
| 503 | { |
Jay Chokshi | de4cefb | 2011-08-04 18:10:44 -0700 | [diff] [blame] | 504 | switch (led->id) { |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 505 | case PM8XXX_ID_LED_0: |
| 506 | case PM8XXX_ID_LED_1: |
| 507 | case PM8XXX_ID_LED_2: |
| 508 | led->cdev.max_brightness = max_current / |
| 509 | PM8XXX_ID_LED_CURRENT_FACTOR; |
Jay Chokshi | de4cefb | 2011-08-04 18:10:44 -0700 | [diff] [blame] | 510 | if (led->cdev.max_brightness > MAX_LC_LED_BRIGHTNESS) |
| 511 | led->cdev.max_brightness = MAX_LC_LED_BRIGHTNESS; |
Willie Ruan | a39d1d4 | 2011-08-19 13:54:34 -0700 | [diff] [blame] | 512 | led->reg = led_mode; |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 513 | break; |
| 514 | case PM8XXX_ID_LED_KB_LIGHT: |
| 515 | case PM8XXX_ID_FLASH_LED_0: |
| 516 | case PM8XXX_ID_FLASH_LED_1: |
| 517 | led->cdev.max_brightness = max_current / |
| 518 | PM8XXX_ID_FLASH_CURRENT_FACTOR; |
Jay Chokshi | de4cefb | 2011-08-04 18:10:44 -0700 | [diff] [blame] | 519 | if (led->cdev.max_brightness > MAX_FLASH_BRIGHTNESS) |
| 520 | led->cdev.max_brightness = MAX_FLASH_BRIGHTNESS; |
| 521 | |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 522 | switch (led_mode) { |
| 523 | case PM8XXX_LED_MODE_PWM1: |
| 524 | case PM8XXX_LED_MODE_PWM2: |
| 525 | case PM8XXX_LED_MODE_PWM3: |
Willie Ruan | a39d1d4 | 2011-08-19 13:54:34 -0700 | [diff] [blame] | 526 | led->reg = PM8XXX_FLASH_MODE_PWM; |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 527 | break; |
| 528 | case PM8XXX_LED_MODE_DTEST1: |
Willie Ruan | a39d1d4 | 2011-08-19 13:54:34 -0700 | [diff] [blame] | 529 | led->reg = PM8XXX_FLASH_MODE_DBUS1; |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 530 | break; |
| 531 | case PM8XXX_LED_MODE_DTEST2: |
Willie Ruan | a39d1d4 | 2011-08-19 13:54:34 -0700 | [diff] [blame] | 532 | led->reg = PM8XXX_FLASH_MODE_DBUS2; |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 533 | break; |
| 534 | default: |
Willie Ruan | a39d1d4 | 2011-08-19 13:54:34 -0700 | [diff] [blame] | 535 | led->reg = PM8XXX_LED_MODE_MANUAL; |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 536 | break; |
| 537 | } |
| 538 | break; |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 539 | case PM8XXX_ID_WLED: |
| 540 | led->cdev.max_brightness = WLED_MAX_LEVEL; |
| 541 | break; |
Amy Maloche | c17c373 | 2012-02-27 18:34:07 -0800 | [diff] [blame] | 542 | case PM8XXX_ID_RGB_LED_RED: |
| 543 | case PM8XXX_ID_RGB_LED_GREEN: |
| 544 | case PM8XXX_ID_RGB_LED_BLUE: |
| 545 | led->cdev.max_brightness = LED_FULL; |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 546 | break; |
Amy Maloche | c17c373 | 2012-02-27 18:34:07 -0800 | [diff] [blame] | 547 | default: |
| 548 | dev_err(led->cdev.dev, "LED Id is invalid"); |
| 549 | return -EINVAL; |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Amy Maloche | c17c373 | 2012-02-27 18:34:07 -0800 | [diff] [blame] | 552 | return 0; |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 553 | } |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 554 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 555 | static enum led_brightness pm8xxx_led_get(struct led_classdev *led_cdev) |
| 556 | { |
| 557 | struct pm8xxx_led_data *led; |
| 558 | |
| 559 | led = container_of(led_cdev, struct pm8xxx_led_data, cdev); |
| 560 | |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 561 | return led->cdev.brightness; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 564 | static int __devinit init_wled(struct pm8xxx_led_data *led) |
| 565 | { |
| 566 | int rc, i; |
| 567 | u8 val, num_wled_strings; |
| 568 | |
| 569 | num_wled_strings = led->wled_cfg->num_strings; |
| 570 | |
| 571 | /* program over voltage protection threshold */ |
| 572 | if (led->wled_cfg->ovp_val > WLED_OVP_37V) { |
| 573 | dev_err(led->dev->parent, "Invalid ovp value"); |
| 574 | return -EINVAL; |
| 575 | } |
| 576 | |
| 577 | rc = pm8xxx_readb(led->dev->parent, WLED_OVP_CFG_REG, &val); |
| 578 | if (rc) { |
| 579 | dev_err(led->dev->parent, "can't read wled ovp config" |
| 580 | " register rc=%d\n", rc); |
| 581 | return rc; |
| 582 | } |
| 583 | |
| 584 | val = (val & ~WLED_OVP_VAL_MASK) | |
| 585 | (led->wled_cfg->ovp_val << WLED_OVP_VAL_BIT_SHFT); |
| 586 | |
| 587 | rc = pm8xxx_writeb(led->dev->parent, WLED_OVP_CFG_REG, val); |
| 588 | if (rc) { |
| 589 | dev_err(led->dev->parent, "can't write wled ovp config" |
| 590 | " register rc=%d\n", rc); |
| 591 | return rc; |
| 592 | } |
| 593 | |
| 594 | /* program current boost limit and output feedback*/ |
| 595 | if (led->wled_cfg->boost_curr_lim > WLED_CURR_LIMIT_1680mA) { |
| 596 | dev_err(led->dev->parent, "Invalid boost current limit"); |
| 597 | return -EINVAL; |
| 598 | } |
| 599 | |
| 600 | rc = pm8xxx_readb(led->dev->parent, WLED_BOOST_CFG_REG, &val); |
| 601 | if (rc) { |
| 602 | dev_err(led->dev->parent, "can't read wled boost config" |
| 603 | " register rc=%d\n", rc); |
| 604 | return rc; |
| 605 | } |
| 606 | |
| 607 | val = (val & ~WLED_BOOST_LIMIT_MASK) | |
| 608 | (led->wled_cfg->boost_curr_lim << WLED_BOOST_LIMIT_BIT_SHFT); |
| 609 | |
| 610 | val = (val & ~WLED_OP_FDBCK_MASK) | |
| 611 | (led->wled_cfg->op_fdbck << WLED_OP_FDBCK_BIT_SHFT); |
| 612 | |
| 613 | rc = pm8xxx_writeb(led->dev->parent, WLED_BOOST_CFG_REG, val); |
| 614 | if (rc) { |
| 615 | dev_err(led->dev->parent, "can't write wled boost config" |
| 616 | " register rc=%d\n", rc); |
| 617 | return rc; |
| 618 | } |
| 619 | |
| 620 | /* program high pole capacitance */ |
| 621 | if (led->wled_cfg->cp_select > WLED_CP_SELECT_MAX) { |
| 622 | dev_err(led->dev->parent, "Invalid pole capacitance"); |
| 623 | return -EINVAL; |
| 624 | } |
| 625 | |
| 626 | rc = pm8xxx_readb(led->dev->parent, WLED_HIGH_POLE_CAP_REG, &val); |
| 627 | if (rc) { |
| 628 | dev_err(led->dev->parent, "can't read wled high pole" |
| 629 | " capacitance register rc=%d\n", rc); |
| 630 | return rc; |
| 631 | } |
| 632 | |
| 633 | val = (val & ~WLED_CP_SELECT_MASK) | led->wled_cfg->cp_select; |
| 634 | |
| 635 | rc = pm8xxx_writeb(led->dev->parent, WLED_HIGH_POLE_CAP_REG, val); |
| 636 | if (rc) { |
| 637 | dev_err(led->dev->parent, "can't write wled high pole" |
| 638 | " capacitance register rc=%d\n", rc); |
| 639 | return rc; |
| 640 | } |
| 641 | |
| 642 | /* program activation delay and maximum current */ |
| 643 | for (i = 0; i < num_wled_strings; i++) { |
| 644 | rc = pm8xxx_readb(led->dev->parent, |
Mohan Pallaka | 58ff405 | 2012-11-19 16:58:59 +0530 | [diff] [blame] | 645 | WLED_MAX_CURR_CFG_REG(i), &val); |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 646 | if (rc) { |
| 647 | dev_err(led->dev->parent, "can't read wled max current" |
| 648 | " config register rc=%d\n", rc); |
| 649 | return rc; |
| 650 | } |
| 651 | |
| 652 | if ((led->wled_cfg->ctrl_delay_us % WLED_CTL_DLY_STEP) || |
| 653 | (led->wled_cfg->ctrl_delay_us > WLED_CTL_DLY_MAX)) { |
| 654 | dev_err(led->dev->parent, "Invalid control delay\n"); |
| 655 | return rc; |
| 656 | } |
| 657 | |
| 658 | val = val / WLED_CTL_DLY_STEP; |
| 659 | val = (val & ~WLED_CTL_DLY_MASK) | |
| 660 | (led->wled_cfg->ctrl_delay_us << WLED_CTL_DLY_BIT_SHFT); |
| 661 | |
| 662 | if ((led->max_current > WLED_MAX_CURR)) { |
| 663 | dev_err(led->dev->parent, "Invalid max current\n"); |
| 664 | return -EINVAL; |
| 665 | } |
| 666 | |
| 667 | val = (val & ~WLED_MAX_CURR_MASK) | led->max_current; |
| 668 | |
| 669 | rc = pm8xxx_writeb(led->dev->parent, |
Mohan Pallaka | 58ff405 | 2012-11-19 16:58:59 +0530 | [diff] [blame] | 670 | WLED_MAX_CURR_CFG_REG(i), val); |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 671 | if (rc) { |
| 672 | dev_err(led->dev->parent, "can't write wled max current" |
| 673 | " config register rc=%d\n", rc); |
| 674 | return rc; |
| 675 | } |
| 676 | } |
| 677 | |
Amy Maloche | c366d5b | 2012-08-28 11:06:40 -0700 | [diff] [blame] | 678 | if (led->wled_cfg->cabc_en) { |
| 679 | rc = pm8xxx_readb(led->dev->parent, WLED_SYNC_REG, &val); |
| 680 | if (rc) { |
| 681 | dev_err(led->dev->parent, |
| 682 | "can't read cabc register rc=%d\n", rc); |
| 683 | return rc; |
| 684 | } |
| 685 | |
| 686 | switch (num_wled_strings) { |
| 687 | case ONE_WLED_STRING: |
| 688 | val |= (WLED_CABC_ONE_STRING << WLED_CABC_SHIFT); |
| 689 | break; |
| 690 | case TWO_WLED_STRINGS: |
| 691 | val |= (WLED_CABC_TWO_STRING << WLED_CABC_SHIFT); |
| 692 | break; |
| 693 | case THREE_WLED_STRINGS: |
| 694 | val |= (WLED_CABC_THREE_STRING << WLED_CABC_SHIFT); |
| 695 | break; |
| 696 | default: |
| 697 | break; |
| 698 | } |
| 699 | |
| 700 | rc = pm8xxx_writeb(led->dev->parent, WLED_SYNC_REG, val); |
| 701 | if (rc) { |
| 702 | dev_err(led->dev->parent, |
| 703 | "can't write to enable cabc rc=%d\n", rc); |
| 704 | return rc; |
| 705 | } |
| 706 | } |
| 707 | |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 708 | /* program digital module generator, cs out and enable the module */ |
| 709 | rc = pm8xxx_readb(led->dev->parent, WLED_MOD_CTRL_REG, &val); |
| 710 | if (rc) { |
| 711 | dev_err(led->dev->parent, "can't read wled module ctrl" |
| 712 | " register rc=%d\n", rc); |
| 713 | return rc; |
| 714 | } |
| 715 | |
| 716 | if (led->wled_cfg->dig_mod_gen_en) |
| 717 | val |= WLED_DIG_MOD_GEN_MASK; |
| 718 | |
| 719 | if (led->wled_cfg->cs_out_en) |
| 720 | val |= WLED_CS_OUT_MASK; |
| 721 | |
| 722 | val |= WLED_EN_MASK; |
| 723 | |
| 724 | rc = pm8xxx_writeb(led->dev->parent, WLED_MOD_CTRL_REG, val); |
| 725 | if (rc) { |
| 726 | dev_err(led->dev->parent, "can't write wled module ctrl" |
| 727 | " register rc=%d\n", rc); |
| 728 | return rc; |
| 729 | } |
Amy Maloche | 56913f5 | 2012-05-11 10:47:24 -0700 | [diff] [blame] | 730 | led->wled_mod_ctrl_val = val; |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 731 | |
| 732 | /* dump wled registers */ |
| 733 | wled_dump_regs(led); |
| 734 | |
| 735 | return 0; |
| 736 | } |
| 737 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 738 | static int __devinit get_init_value(struct pm8xxx_led_data *led, u8 *val) |
| 739 | { |
| 740 | int rc, offset; |
| 741 | u16 addr; |
| 742 | |
| 743 | switch (led->id) { |
| 744 | case PM8XXX_ID_LED_KB_LIGHT: |
| 745 | addr = SSBI_REG_ADDR_DRV_KEYPAD; |
| 746 | break; |
| 747 | case PM8XXX_ID_LED_0: |
| 748 | case PM8XXX_ID_LED_1: |
| 749 | case PM8XXX_ID_LED_2: |
| 750 | offset = PM8XXX_LED_OFFSET(led->id); |
| 751 | addr = SSBI_REG_ADDR_LED_CTRL(offset); |
| 752 | break; |
| 753 | case PM8XXX_ID_FLASH_LED_0: |
| 754 | addr = SSBI_REG_ADDR_FLASH_DRV0; |
| 755 | break; |
| 756 | case PM8XXX_ID_FLASH_LED_1: |
| 757 | addr = SSBI_REG_ADDR_FLASH_DRV1; |
| 758 | break; |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 759 | case PM8XXX_ID_WLED: |
| 760 | rc = init_wled(led); |
| 761 | if (rc) |
| 762 | dev_err(led->cdev.dev, "can't initialize wled rc=%d\n", |
| 763 | rc); |
| 764 | return rc; |
Amy Maloche | c17c373 | 2012-02-27 18:34:07 -0800 | [diff] [blame] | 765 | case PM8XXX_ID_RGB_LED_RED: |
| 766 | case PM8XXX_ID_RGB_LED_GREEN: |
| 767 | case PM8XXX_ID_RGB_LED_BLUE: |
Amy Maloche | c17c373 | 2012-02-27 18:34:07 -0800 | [diff] [blame] | 768 | addr = SSBI_REG_ADDR_RGB_CNTL1; |
| 769 | break; |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 770 | default: |
| 771 | dev_err(led->cdev.dev, "unknown led id %d", led->id); |
| 772 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | rc = pm8xxx_readb(led->dev->parent, addr, val); |
| 776 | if (rc) |
| 777 | dev_err(led->cdev.dev, "can't get led(%d) level rc=%d\n", |
| 778 | led->id, rc); |
| 779 | |
| 780 | return rc; |
| 781 | } |
| 782 | |
Jay Chokshi | 868312e | 2011-09-16 13:57:13 -0700 | [diff] [blame] | 783 | static int pm8xxx_led_pwm_configure(struct pm8xxx_led_data *led) |
| 784 | { |
| 785 | int start_idx, idx_len, duty_us, rc; |
| 786 | |
| 787 | led->pwm_dev = pwm_request(led->pwm_channel, |
| 788 | led->cdev.name); |
| 789 | |
| 790 | if (IS_ERR_OR_NULL(led->pwm_dev)) { |
| 791 | pr_err("could not acquire PWM Channel %d, " |
| 792 | "error %ld\n", led->pwm_channel, |
| 793 | PTR_ERR(led->pwm_dev)); |
| 794 | led->pwm_dev = NULL; |
| 795 | return -ENODEV; |
| 796 | } |
| 797 | |
| 798 | if (led->pwm_duty_cycles != NULL) { |
| 799 | start_idx = led->pwm_duty_cycles->start_idx; |
| 800 | idx_len = led->pwm_duty_cycles->num_duty_pcts; |
| 801 | |
| 802 | if (idx_len >= PM_PWM_LUT_SIZE && start_idx) { |
| 803 | pr_err("Wrong LUT size or index\n"); |
| 804 | return -EINVAL; |
| 805 | } |
| 806 | if ((start_idx + idx_len) > PM_PWM_LUT_SIZE) { |
| 807 | pr_err("Exceed LUT limit\n"); |
| 808 | return -EINVAL; |
| 809 | } |
| 810 | |
| 811 | rc = pm8xxx_pwm_lut_config(led->pwm_dev, led->pwm_period_us, |
| 812 | led->pwm_duty_cycles->duty_pcts, |
| 813 | led->pwm_duty_cycles->duty_ms, |
| 814 | start_idx, idx_len, 0, 0, |
| 815 | PM8XXX_LED_PWM_FLAGS); |
| 816 | } else { |
| 817 | duty_us = led->pwm_period_us; |
| 818 | rc = pwm_config(led->pwm_dev, duty_us, led->pwm_period_us); |
| 819 | } |
| 820 | |
| 821 | return rc; |
| 822 | } |
| 823 | |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 824 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 825 | static int __devinit pm8xxx_led_probe(struct platform_device *pdev) |
| 826 | { |
Jay Chokshi | 8994e39 | 2011-09-14 18:20:39 -0700 | [diff] [blame] | 827 | const struct pm8xxx_led_platform_data *pdata = pdev->dev.platform_data; |
| 828 | const struct led_platform_data *pcore_data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 829 | struct led_info *curr_led; |
| 830 | struct pm8xxx_led_data *led, *led_dat; |
Jay Chokshi | 8994e39 | 2011-09-14 18:20:39 -0700 | [diff] [blame] | 831 | struct pm8xxx_led_config *led_cfg; |
Amy Maloche | 3d2c24c | 2012-03-22 19:02:25 -0700 | [diff] [blame] | 832 | enum pm8xxx_version version; |
| 833 | bool found = false; |
| 834 | int rc, i, j; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 835 | |
| 836 | if (pdata == NULL) { |
| 837 | dev_err(&pdev->dev, "platform data not supplied\n"); |
| 838 | return -EINVAL; |
| 839 | } |
| 840 | |
Jay Chokshi | 8994e39 | 2011-09-14 18:20:39 -0700 | [diff] [blame] | 841 | pcore_data = pdata->led_core; |
| 842 | |
| 843 | if (pcore_data->num_leds != pdata->num_configs) { |
| 844 | dev_err(&pdev->dev, "#no. of led configs and #no. of led" |
| 845 | "entries are not equal\n"); |
| 846 | return -EINVAL; |
| 847 | } |
| 848 | |
| 849 | led = kcalloc(pcore_data->num_leds, sizeof(*led), GFP_KERNEL); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 850 | if (led == NULL) { |
| 851 | dev_err(&pdev->dev, "failed to alloc memory\n"); |
| 852 | return -ENOMEM; |
| 853 | } |
| 854 | |
Jay Chokshi | 8994e39 | 2011-09-14 18:20:39 -0700 | [diff] [blame] | 855 | for (i = 0; i < pcore_data->num_leds; i++) { |
| 856 | curr_led = &pcore_data->leds[i]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 857 | led_dat = &led[i]; |
Jay Chokshi | 8994e39 | 2011-09-14 18:20:39 -0700 | [diff] [blame] | 858 | led_cfg = &pdata->configs[i]; |
| 859 | |
| 860 | led_dat->id = led_cfg->id; |
Jay Chokshi | 868312e | 2011-09-16 13:57:13 -0700 | [diff] [blame] | 861 | led_dat->pwm_channel = led_cfg->pwm_channel; |
| 862 | led_dat->pwm_period_us = led_cfg->pwm_period_us; |
| 863 | led_dat->pwm_duty_cycles = led_cfg->pwm_duty_cycles; |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 864 | led_dat->wled_cfg = led_cfg->wled_cfg; |
| 865 | led_dat->max_current = led_cfg->max_current; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 866 | |
| 867 | if (!((led_dat->id >= PM8XXX_ID_LED_KB_LIGHT) && |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 868 | (led_dat->id < PM8XXX_ID_MAX))) { |
Amy Maloche | 3d2c24c | 2012-03-22 19:02:25 -0700 | [diff] [blame] | 869 | dev_err(&pdev->dev, "invalid LED ID(%d) specified\n", |
| 870 | led_dat->id); |
| 871 | rc = -EINVAL; |
| 872 | goto fail_id_check; |
| 873 | |
| 874 | } |
| 875 | |
| 876 | found = false; |
| 877 | version = pm8xxx_get_version(pdev->dev.parent); |
| 878 | for (j = 0; j < ARRAY_SIZE(led_map); j++) { |
| 879 | if (version == led_map[j].version |
| 880 | && (led_map[j].supported & (1 << led_dat->id))) { |
| 881 | found = true; |
| 882 | break; |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | if (!found) { |
| 887 | dev_err(&pdev->dev, "invalid LED ID(%d) specified\n", |
| 888 | led_dat->id); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 889 | rc = -EINVAL; |
| 890 | goto fail_id_check; |
| 891 | } |
| 892 | |
| 893 | led_dat->cdev.name = curr_led->name; |
| 894 | led_dat->cdev.default_trigger = curr_led->default_trigger; |
| 895 | led_dat->cdev.brightness_set = pm8xxx_led_set; |
| 896 | led_dat->cdev.brightness_get = pm8xxx_led_get; |
| 897 | led_dat->cdev.brightness = LED_OFF; |
Jay Chokshi | 8994e39 | 2011-09-14 18:20:39 -0700 | [diff] [blame] | 898 | led_dat->cdev.flags = curr_led->flags; |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 899 | led_dat->dev = &pdev->dev; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 900 | |
| 901 | rc = get_init_value(led_dat, &led_dat->reg); |
| 902 | if (rc < 0) |
| 903 | goto fail_id_check; |
| 904 | |
Jay Chokshi | 8994e39 | 2011-09-14 18:20:39 -0700 | [diff] [blame] | 905 | rc = pm8xxx_set_led_mode_and_max_brightness(led_dat, |
| 906 | led_cfg->mode, led_cfg->max_current); |
Jay Chokshi | de4cefb | 2011-08-04 18:10:44 -0700 | [diff] [blame] | 907 | if (rc < 0) |
| 908 | goto fail_id_check; |
| 909 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 910 | mutex_init(&led_dat->lock); |
| 911 | INIT_WORK(&led_dat->work, pm8xxx_led_work); |
| 912 | |
| 913 | rc = led_classdev_register(&pdev->dev, &led_dat->cdev); |
| 914 | if (rc) { |
| 915 | dev_err(&pdev->dev, "unable to register led %d,rc=%d\n", |
| 916 | led_dat->id, rc); |
| 917 | goto fail_id_check; |
| 918 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 919 | |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 920 | /* configure default state */ |
| 921 | if (led_cfg->default_state) |
| 922 | led->cdev.brightness = led_dat->cdev.max_brightness; |
| 923 | else |
| 924 | led->cdev.brightness = LED_OFF; |
| 925 | |
Jay Chokshi | 868312e | 2011-09-16 13:57:13 -0700 | [diff] [blame] | 926 | if (led_cfg->mode != PM8XXX_LED_MODE_MANUAL) { |
Amy Maloche | c17c373 | 2012-02-27 18:34:07 -0800 | [diff] [blame] | 927 | if (led_dat->id == PM8XXX_ID_RGB_LED_RED || |
| 928 | led_dat->id == PM8XXX_ID_RGB_LED_GREEN || |
| 929 | led_dat->id == PM8XXX_ID_RGB_LED_BLUE) |
| 930 | __pm8xxx_led_work(led_dat, 0); |
| 931 | else |
| 932 | __pm8xxx_led_work(led_dat, |
Jay Chokshi | de4cefb | 2011-08-04 18:10:44 -0700 | [diff] [blame] | 933 | led_dat->cdev.max_brightness); |
Jay Chokshi | 868312e | 2011-09-16 13:57:13 -0700 | [diff] [blame] | 934 | |
| 935 | if (led_dat->pwm_channel != -1) { |
| 936 | led_dat->cdev.max_brightness = LED_FULL; |
| 937 | rc = pm8xxx_led_pwm_configure(led_dat); |
| 938 | if (rc) { |
| 939 | dev_err(&pdev->dev, "failed to " |
| 940 | "configure LED, error: %d\n", rc); |
| 941 | goto fail_id_check; |
| 942 | } |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 943 | schedule_work(&led->work); |
Jay Chokshi | 868312e | 2011-09-16 13:57:13 -0700 | [diff] [blame] | 944 | } |
| 945 | } else { |
Mohan Pallaka | 53fe1a1 | 2011-12-15 16:56:37 +0530 | [diff] [blame] | 946 | __pm8xxx_led_work(led_dat, led->cdev.brightness); |
Jay Chokshi | 868312e | 2011-09-16 13:57:13 -0700 | [diff] [blame] | 947 | } |
Jay Chokshi | de4cefb | 2011-08-04 18:10:44 -0700 | [diff] [blame] | 948 | } |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 949 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 950 | platform_set_drvdata(pdev, led); |
| 951 | |
| 952 | return 0; |
| 953 | |
| 954 | fail_id_check: |
| 955 | if (i > 0) { |
| 956 | for (i = i - 1; i >= 0; i--) { |
| 957 | mutex_destroy(&led[i].lock); |
| 958 | led_classdev_unregister(&led[i].cdev); |
Jay Chokshi | 868312e | 2011-09-16 13:57:13 -0700 | [diff] [blame] | 959 | if (led[i].pwm_dev != NULL) |
| 960 | pwm_free(led[i].pwm_dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 961 | } |
| 962 | } |
| 963 | kfree(led); |
| 964 | return rc; |
| 965 | } |
| 966 | |
| 967 | static int __devexit pm8xxx_led_remove(struct platform_device *pdev) |
| 968 | { |
| 969 | int i; |
| 970 | const struct led_platform_data *pdata = |
| 971 | pdev->dev.platform_data; |
| 972 | struct pm8xxx_led_data *led = platform_get_drvdata(pdev); |
| 973 | |
| 974 | for (i = 0; i < pdata->num_leds; i++) { |
| 975 | cancel_work_sync(&led[i].work); |
| 976 | mutex_destroy(&led[i].lock); |
| 977 | led_classdev_unregister(&led[i].cdev); |
Jay Chokshi | 868312e | 2011-09-16 13:57:13 -0700 | [diff] [blame] | 978 | if (led[i].pwm_dev != NULL) |
| 979 | pwm_free(led[i].pwm_dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | kfree(led); |
| 983 | |
| 984 | return 0; |
| 985 | } |
| 986 | |
| 987 | static struct platform_driver pm8xxx_led_driver = { |
| 988 | .probe = pm8xxx_led_probe, |
| 989 | .remove = __devexit_p(pm8xxx_led_remove), |
| 990 | .driver = { |
| 991 | .name = PM8XXX_LEDS_DEV_NAME, |
| 992 | .owner = THIS_MODULE, |
| 993 | }, |
| 994 | }; |
| 995 | |
| 996 | static int __init pm8xxx_led_init(void) |
| 997 | { |
| 998 | return platform_driver_register(&pm8xxx_led_driver); |
| 999 | } |
Jay Chokshi | 12e49bf | 2011-07-22 16:24:39 -0700 | [diff] [blame] | 1000 | subsys_initcall(pm8xxx_led_init); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1001 | |
| 1002 | static void __exit pm8xxx_led_exit(void) |
| 1003 | { |
| 1004 | platform_driver_unregister(&pm8xxx_led_driver); |
| 1005 | } |
| 1006 | module_exit(pm8xxx_led_exit); |
| 1007 | |
| 1008 | MODULE_DESCRIPTION("PM8XXX LEDs driver"); |
| 1009 | MODULE_LICENSE("GPL v2"); |
| 1010 | MODULE_VERSION("1.0"); |
| 1011 | MODULE_ALIAS("platform:pm8xxx-led"); |