Duy Truong | 790f06d | 2013-02-13 16:38:12 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010, 2011, The Linux Foundation. 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 | #include <linux/kernel.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/leds.h> |
| 16 | #include <linux/workqueue.h> |
| 17 | #include <linux/spinlock.h> |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 18 | #include <linux/mfd/pm8xxx/core.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 19 | #include <linux/leds-pmic8058.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 20 | #include <linux/module.h> |
| 21 | #include <linux/string.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 22 | |
| 23 | #define SSBI_REG_ADDR_DRV_KEYPAD 0x48 |
| 24 | #define PM8058_DRV_KEYPAD_BL_MASK 0xf0 |
| 25 | #define PM8058_DRV_KEYPAD_BL_SHIFT 0x04 |
| 26 | |
| 27 | #define SSBI_REG_ADDR_FLASH_DRV0 0x49 |
| 28 | #define PM8058_DRV_FLASH_MASK 0xf0 |
| 29 | #define PM8058_DRV_FLASH_SHIFT 0x04 |
| 30 | |
| 31 | #define SSBI_REG_ADDR_FLASH_DRV1 0xFB |
| 32 | |
| 33 | #define SSBI_REG_ADDR_LED_CTRL_BASE 0x131 |
| 34 | #define SSBI_REG_ADDR_LED_CTRL(n) (SSBI_REG_ADDR_LED_CTRL_BASE + (n)) |
| 35 | #define PM8058_DRV_LED_CTRL_MASK 0xf8 |
| 36 | #define PM8058_DRV_LED_CTRL_SHIFT 0x03 |
| 37 | |
| 38 | #define MAX_FLASH_CURRENT 300 |
| 39 | #define MAX_KEYPAD_CURRENT 300 |
| 40 | #define MAX_KEYPAD_BL_LEVEL (1 << 4) |
| 41 | #define MAX_LED_DRV_LEVEL 20 /* 2 * 20 mA */ |
| 42 | |
| 43 | #define PMIC8058_LED_OFFSET(id) ((id) - PMIC8058_ID_LED_0) |
| 44 | |
| 45 | struct pmic8058_led_data { |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 46 | struct device *dev; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 47 | struct led_classdev cdev; |
| 48 | int id; |
| 49 | enum led_brightness brightness; |
| 50 | u8 flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 51 | struct work_struct work; |
| 52 | struct mutex lock; |
| 53 | spinlock_t value_lock; |
| 54 | u8 reg_kp; |
| 55 | u8 reg_led_ctrl[3]; |
| 56 | u8 reg_flash_led0; |
| 57 | u8 reg_flash_led1; |
| 58 | }; |
| 59 | |
| 60 | #define PM8058_MAX_LEDS 7 |
| 61 | static struct pmic8058_led_data led_data[PM8058_MAX_LEDS]; |
| 62 | |
| 63 | static void kp_bl_set(struct pmic8058_led_data *led, enum led_brightness value) |
| 64 | { |
| 65 | int rc; |
| 66 | u8 level; |
| 67 | unsigned long flags; |
| 68 | |
| 69 | spin_lock_irqsave(&led->value_lock, flags); |
| 70 | level = (value << PM8058_DRV_KEYPAD_BL_SHIFT) & |
| 71 | PM8058_DRV_KEYPAD_BL_MASK; |
| 72 | |
| 73 | led->reg_kp &= ~PM8058_DRV_KEYPAD_BL_MASK; |
| 74 | led->reg_kp |= level; |
| 75 | spin_unlock_irqrestore(&led->value_lock, flags); |
| 76 | |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 77 | rc = pm8xxx_writeb(led->dev->parent, SSBI_REG_ADDR_DRV_KEYPAD, |
| 78 | led->reg_kp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 79 | if (rc) |
| 80 | pr_err("%s: can't set keypad backlight level\n", __func__); |
| 81 | } |
| 82 | |
| 83 | static enum led_brightness kp_bl_get(struct pmic8058_led_data *led) |
| 84 | { |
| 85 | if ((led->reg_kp & PM8058_DRV_KEYPAD_BL_MASK) >> |
| 86 | PM8058_DRV_KEYPAD_BL_SHIFT) |
| 87 | return LED_FULL; |
| 88 | else |
| 89 | return LED_OFF; |
| 90 | } |
| 91 | |
| 92 | static void led_lc_set(struct pmic8058_led_data *led, enum led_brightness value) |
| 93 | { |
| 94 | unsigned long flags; |
| 95 | int rc, offset; |
| 96 | u8 level, tmp; |
| 97 | |
| 98 | spin_lock_irqsave(&led->value_lock, flags); |
| 99 | |
| 100 | level = (led->brightness << PM8058_DRV_LED_CTRL_SHIFT) & |
| 101 | PM8058_DRV_LED_CTRL_MASK; |
| 102 | |
| 103 | offset = PMIC8058_LED_OFFSET(led->id); |
| 104 | tmp = led->reg_led_ctrl[offset]; |
| 105 | |
| 106 | tmp &= ~PM8058_DRV_LED_CTRL_MASK; |
| 107 | tmp |= level; |
| 108 | spin_unlock_irqrestore(&led->value_lock, flags); |
| 109 | |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 110 | rc = pm8xxx_writeb(led->dev->parent, SSBI_REG_ADDR_LED_CTRL(offset), |
| 111 | tmp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 112 | if (rc) { |
| 113 | dev_err(led->cdev.dev, "can't set (%d) led value\n", |
| 114 | led->id); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | spin_lock_irqsave(&led->value_lock, flags); |
| 119 | led->reg_led_ctrl[offset] = tmp; |
| 120 | spin_unlock_irqrestore(&led->value_lock, flags); |
| 121 | } |
| 122 | |
| 123 | static enum led_brightness led_lc_get(struct pmic8058_led_data *led) |
| 124 | { |
| 125 | int offset; |
| 126 | u8 value; |
| 127 | |
| 128 | offset = PMIC8058_LED_OFFSET(led->id); |
| 129 | value = led->reg_led_ctrl[offset]; |
| 130 | |
| 131 | if ((value & PM8058_DRV_LED_CTRL_MASK) >> |
| 132 | PM8058_DRV_LED_CTRL_SHIFT) |
| 133 | return LED_FULL; |
| 134 | else |
| 135 | return LED_OFF; |
| 136 | } |
| 137 | |
| 138 | static void |
| 139 | led_flash_set(struct pmic8058_led_data *led, enum led_brightness value) |
| 140 | { |
| 141 | int rc; |
| 142 | u8 level; |
| 143 | unsigned long flags; |
| 144 | u8 reg_flash_led; |
| 145 | u16 reg_addr; |
| 146 | |
| 147 | spin_lock_irqsave(&led->value_lock, flags); |
| 148 | level = (value << PM8058_DRV_FLASH_SHIFT) & |
| 149 | PM8058_DRV_FLASH_MASK; |
| 150 | |
| 151 | if (led->id == PMIC8058_ID_FLASH_LED_0) { |
| 152 | led->reg_flash_led0 &= ~PM8058_DRV_FLASH_MASK; |
| 153 | led->reg_flash_led0 |= level; |
| 154 | reg_flash_led = led->reg_flash_led0; |
| 155 | reg_addr = SSBI_REG_ADDR_FLASH_DRV0; |
| 156 | } else { |
| 157 | led->reg_flash_led1 &= ~PM8058_DRV_FLASH_MASK; |
| 158 | led->reg_flash_led1 |= level; |
| 159 | reg_flash_led = led->reg_flash_led1; |
| 160 | reg_addr = SSBI_REG_ADDR_FLASH_DRV1; |
| 161 | } |
| 162 | spin_unlock_irqrestore(&led->value_lock, flags); |
| 163 | |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 164 | rc = pm8xxx_writeb(led->dev->parent, reg_addr, reg_flash_led); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 165 | if (rc) |
| 166 | pr_err("%s: can't set flash led%d level %d\n", __func__, |
| 167 | led->id, rc); |
| 168 | } |
| 169 | |
| 170 | int pm8058_set_flash_led_current(enum pmic8058_leds id, unsigned mA) |
| 171 | { |
| 172 | struct pmic8058_led_data *led; |
| 173 | |
| 174 | if ((id < PMIC8058_ID_FLASH_LED_0) || (id > PMIC8058_ID_FLASH_LED_1)) { |
| 175 | pr_err("%s: invalid LED ID (%d) specified\n", __func__, id); |
| 176 | return -EINVAL; |
| 177 | } |
| 178 | |
| 179 | led = &led_data[id]; |
| 180 | if (!led) { |
| 181 | pr_err("%s: flash led not available\n", __func__); |
| 182 | return -EINVAL; |
| 183 | } |
| 184 | |
| 185 | if (mA > MAX_FLASH_CURRENT) |
| 186 | return -EINVAL; |
| 187 | |
| 188 | led_flash_set(led, mA / 20); |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | EXPORT_SYMBOL(pm8058_set_flash_led_current); |
| 193 | |
| 194 | int pm8058_set_led_current(enum pmic8058_leds id, unsigned mA) |
| 195 | { |
| 196 | struct pmic8058_led_data *led; |
| 197 | int brightness = 0; |
| 198 | |
| 199 | if ((id < PMIC8058_ID_LED_KB_LIGHT) || (id > PMIC8058_ID_FLASH_LED_1)) { |
| 200 | pr_err("%s: invalid LED ID (%d) specified\n", __func__, id); |
| 201 | return -EINVAL; |
| 202 | } |
| 203 | |
| 204 | led = &led_data[id]; |
| 205 | if (!led) { |
| 206 | pr_err("%s: flash led not available\n", __func__); |
| 207 | return -EINVAL; |
| 208 | } |
| 209 | |
| 210 | switch (id) { |
| 211 | case PMIC8058_ID_LED_0: |
| 212 | case PMIC8058_ID_LED_1: |
| 213 | case PMIC8058_ID_LED_2: |
| 214 | brightness = mA / 2; |
| 215 | if (brightness > led->cdev.max_brightness) |
| 216 | return -EINVAL; |
| 217 | led_lc_set(led, brightness); |
| 218 | break; |
| 219 | |
| 220 | case PMIC8058_ID_LED_KB_LIGHT: |
| 221 | case PMIC8058_ID_FLASH_LED_0: |
| 222 | case PMIC8058_ID_FLASH_LED_1: |
| 223 | brightness = mA / 20; |
| 224 | if (brightness > led->cdev.max_brightness) |
| 225 | return -EINVAL; |
| 226 | if (id == PMIC8058_ID_LED_KB_LIGHT) |
| 227 | kp_bl_set(led, brightness); |
| 228 | else |
| 229 | led_flash_set(led, brightness); |
| 230 | break; |
| 231 | } |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | EXPORT_SYMBOL(pm8058_set_led_current); |
| 236 | |
| 237 | static void pmic8058_led_set(struct led_classdev *led_cdev, |
| 238 | enum led_brightness value) |
| 239 | { |
| 240 | struct pmic8058_led_data *led; |
| 241 | unsigned long flags; |
| 242 | |
| 243 | led = container_of(led_cdev, struct pmic8058_led_data, cdev); |
| 244 | |
| 245 | spin_lock_irqsave(&led->value_lock, flags); |
| 246 | led->brightness = value; |
| 247 | schedule_work(&led->work); |
| 248 | spin_unlock_irqrestore(&led->value_lock, flags); |
| 249 | } |
| 250 | |
| 251 | static void pmic8058_led_work(struct work_struct *work) |
| 252 | { |
| 253 | struct pmic8058_led_data *led = container_of(work, |
| 254 | struct pmic8058_led_data, work); |
| 255 | |
| 256 | mutex_lock(&led->lock); |
| 257 | |
| 258 | switch (led->id) { |
| 259 | case PMIC8058_ID_LED_KB_LIGHT: |
| 260 | kp_bl_set(led, led->brightness); |
| 261 | break; |
| 262 | case PMIC8058_ID_LED_0: |
| 263 | case PMIC8058_ID_LED_1: |
| 264 | case PMIC8058_ID_LED_2: |
| 265 | led_lc_set(led, led->brightness); |
| 266 | break; |
| 267 | case PMIC8058_ID_FLASH_LED_0: |
| 268 | case PMIC8058_ID_FLASH_LED_1: |
| 269 | led_flash_set(led, led->brightness); |
| 270 | break; |
| 271 | } |
| 272 | |
| 273 | mutex_unlock(&led->lock); |
| 274 | } |
| 275 | |
| 276 | static enum led_brightness pmic8058_led_get(struct led_classdev *led_cdev) |
| 277 | { |
| 278 | struct pmic8058_led_data *led; |
| 279 | |
| 280 | led = container_of(led_cdev, struct pmic8058_led_data, cdev); |
| 281 | |
| 282 | switch (led->id) { |
| 283 | case PMIC8058_ID_LED_KB_LIGHT: |
| 284 | return kp_bl_get(led); |
| 285 | case PMIC8058_ID_LED_0: |
| 286 | case PMIC8058_ID_LED_1: |
| 287 | case PMIC8058_ID_LED_2: |
| 288 | return led_lc_get(led); |
| 289 | } |
| 290 | return LED_OFF; |
| 291 | } |
| 292 | |
| 293 | static int pmic8058_led_probe(struct platform_device *pdev) |
| 294 | { |
| 295 | struct pmic8058_leds_platform_data *pdata = pdev->dev.platform_data; |
| 296 | struct pmic8058_led_data *led_dat; |
| 297 | struct pmic8058_led *curr_led; |
| 298 | int rc, i = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 299 | u8 reg_kp; |
| 300 | u8 reg_led_ctrl[3]; |
| 301 | u8 reg_flash_led0; |
| 302 | u8 reg_flash_led1; |
| 303 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 304 | if (pdata == NULL) { |
| 305 | dev_err(&pdev->dev, "platform data not supplied\n"); |
| 306 | return -EINVAL; |
| 307 | } |
| 308 | |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 309 | rc = pm8xxx_readb(pdev->dev.parent, SSBI_REG_ADDR_DRV_KEYPAD, ®_kp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 310 | if (rc) { |
| 311 | dev_err(&pdev->dev, "can't get keypad backlight level\n"); |
| 312 | goto err_reg_read; |
| 313 | } |
| 314 | |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 315 | rc = pm8xxx_read_buf(pdev->dev.parent, SSBI_REG_ADDR_LED_CTRL_BASE, |
| 316 | reg_led_ctrl, 3); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 317 | if (rc) { |
| 318 | dev_err(&pdev->dev, "can't get led levels\n"); |
| 319 | goto err_reg_read; |
| 320 | } |
| 321 | |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 322 | rc = pm8xxx_readb(pdev->dev.parent, SSBI_REG_ADDR_FLASH_DRV0, |
| 323 | ®_flash_led0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 324 | if (rc) { |
| 325 | dev_err(&pdev->dev, "can't read flash led0\n"); |
| 326 | goto err_reg_read; |
| 327 | } |
| 328 | |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 329 | rc = pm8xxx_readb(pdev->dev.parent, SSBI_REG_ADDR_FLASH_DRV1, |
| 330 | ®_flash_led1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 331 | if (rc) { |
| 332 | dev_err(&pdev->dev, "can't get flash led1\n"); |
| 333 | goto err_reg_read; |
| 334 | } |
| 335 | |
| 336 | for (i = 0; i < pdata->num_leds; i++) { |
| 337 | curr_led = &pdata->leds[i]; |
| 338 | led_dat = &led_data[curr_led->id]; |
| 339 | |
| 340 | led_dat->cdev.name = curr_led->name; |
| 341 | led_dat->cdev.default_trigger = curr_led->default_trigger; |
| 342 | led_dat->cdev.brightness_set = pmic8058_led_set; |
| 343 | led_dat->cdev.brightness_get = pmic8058_led_get; |
| 344 | led_dat->cdev.brightness = LED_OFF; |
| 345 | led_dat->cdev.max_brightness = curr_led->max_brightness; |
| 346 | led_dat->cdev.flags = LED_CORE_SUSPENDRESUME; |
| 347 | |
| 348 | led_dat->id = curr_led->id; |
| 349 | led_dat->reg_kp = reg_kp; |
| 350 | memcpy(led_data->reg_led_ctrl, reg_led_ctrl, |
| 351 | sizeof(reg_led_ctrl)); |
| 352 | led_dat->reg_flash_led0 = reg_flash_led0; |
| 353 | led_dat->reg_flash_led1 = reg_flash_led1; |
| 354 | |
| 355 | if (!((led_dat->id >= PMIC8058_ID_LED_KB_LIGHT) && |
| 356 | (led_dat->id <= PMIC8058_ID_FLASH_LED_1))) { |
| 357 | dev_err(&pdev->dev, "invalid LED ID (%d) specified\n", |
| 358 | led_dat->id); |
| 359 | rc = -EINVAL; |
| 360 | goto fail_id_check; |
| 361 | } |
| 362 | |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 363 | led_dat->dev = &pdev->dev; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 364 | |
| 365 | mutex_init(&led_dat->lock); |
| 366 | spin_lock_init(&led_dat->value_lock); |
| 367 | INIT_WORK(&led_dat->work, pmic8058_led_work); |
| 368 | |
| 369 | rc = led_classdev_register(&pdev->dev, &led_dat->cdev); |
| 370 | if (rc) { |
| 371 | dev_err(&pdev->dev, "unable to register led %d\n", |
| 372 | led_dat->id); |
| 373 | goto fail_id_check; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | platform_set_drvdata(pdev, led_data); |
| 378 | |
| 379 | return 0; |
| 380 | |
| 381 | err_reg_read: |
| 382 | fail_id_check: |
| 383 | if (i > 0) { |
| 384 | for (i = i - 1; i >= 0; i--) |
| 385 | led_classdev_unregister(&led_data[i].cdev); |
| 386 | } |
| 387 | return rc; |
| 388 | } |
| 389 | |
| 390 | static int __devexit pmic8058_led_remove(struct platform_device *pdev) |
| 391 | { |
| 392 | int i; |
| 393 | struct pmic8058_leds_platform_data *pdata = pdev->dev.platform_data; |
| 394 | struct pmic8058_led_data *led = platform_get_drvdata(pdev); |
| 395 | |
| 396 | for (i = 0; i < pdata->num_leds; i++) { |
| 397 | led_classdev_unregister(&led[led->id].cdev); |
| 398 | cancel_work_sync(&led[led->id].work); |
| 399 | } |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | static struct platform_driver pmic8058_led_driver = { |
| 405 | .probe = pmic8058_led_probe, |
| 406 | .remove = __devexit_p(pmic8058_led_remove), |
| 407 | .driver = { |
| 408 | .name = "pm8058-led", |
| 409 | .owner = THIS_MODULE, |
| 410 | }, |
| 411 | }; |
| 412 | |
| 413 | static int __init pmic8058_led_init(void) |
| 414 | { |
| 415 | return platform_driver_register(&pmic8058_led_driver); |
| 416 | } |
| 417 | module_init(pmic8058_led_init); |
| 418 | |
| 419 | static void __exit pmic8058_led_exit(void) |
| 420 | { |
| 421 | platform_driver_unregister(&pmic8058_led_driver); |
| 422 | } |
| 423 | module_exit(pmic8058_led_exit); |
| 424 | |
| 425 | MODULE_DESCRIPTION("PMIC8058 LEDs driver"); |
| 426 | MODULE_LICENSE("GPL v2"); |
| 427 | MODULE_VERSION("1.0"); |
| 428 | MODULE_ALIAS("platform:pmic8058-led"); |