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