| /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved. |
| * |
| * This program is free software; you can redistribute it and/or modify |
| * it under the terms of the GNU General Public License version 2 and |
| * only version 2 as published by the Free Software Foundation. |
| * |
| * This program is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| * GNU General Public License for more details. |
| */ |
| |
| #define pr_fmt(fmt) "%s: " fmt, __func__ |
| |
| #include <linux/kernel.h> |
| #include <linux/init.h> |
| #include <linux/slab.h> |
| #include <linux/platform_device.h> |
| #include <linux/leds.h> |
| #include <linux/workqueue.h> |
| |
| #include <linux/mfd/pm8xxx/core.h> |
| #include <linux/leds-pm8xxx.h> |
| |
| #define SSBI_REG_ADDR_DRV_KEYPAD 0x48 |
| #define PM8XXX_DRV_KEYPAD_BL_MASK 0xf0 |
| #define PM8XXX_DRV_KEYPAD_BL_SHIFT 0x04 |
| |
| #define SSBI_REG_ADDR_FLASH_DRV0 0x49 |
| #define PM8XXX_DRV_FLASH_MASK 0xf0 |
| #define PM8XXX_DRV_FLASH_SHIFT 0x04 |
| |
| #define SSBI_REG_ADDR_FLASH_DRV1 0xFB |
| |
| #define SSBI_REG_ADDR_LED_CTRL_BASE 0x131 |
| #define SSBI_REG_ADDR_LED_CTRL(n) (SSBI_REG_ADDR_LED_CTRL_BASE + (n)) |
| #define PM8XXX_DRV_LED_CTRL_MASK 0xf8 |
| #define PM8XXX_DRV_LED_CTRL_SHIFT 0x03 |
| |
| #define MAX_FLASH_LED_CURRENT 300 |
| #define MAX_LC_LED_CURRENT 40 |
| #define MAX_KP_BL_LED_CURRENT 300 |
| |
| #define PM8XXX_ID_LED_CURRENT_FACTOR 2 /* Iout = x * 2mA */ |
| #define PM8XXX_ID_FLASH_CURRENT_FACTOR 20 /* Iout = x * 20mA */ |
| |
| #define PM8XXX_FLASH_MODE_DBUS1 1 |
| #define PM8XXX_FLASH_MODE_DBUS2 2 |
| #define PM8XXX_FLASH_MODE_PWM 3 |
| |
| #define MAX_LC_LED_BRIGHTNESS 20 |
| #define MAX_FLASH_BRIGHTNESS 15 |
| #define MAX_KB_LED_BRIGHTNESS 15 |
| |
| #define PM8XXX_LED_OFFSET(id) ((id) - PM8XXX_ID_LED_0) |
| |
| /** |
| * struct pm8xxx_led_data - internal led data structure |
| * @led_classdev - led class device |
| * @id - led index |
| * @work - workqueue for led |
| * @lock - to protect the transactions |
| * @reg - cached value of led register |
| */ |
| struct pm8xxx_led_data { |
| struct led_classdev cdev; |
| int id; |
| u8 reg; |
| struct device *dev; |
| struct work_struct work; |
| struct mutex lock; |
| }; |
| |
| static void led_kp_set(struct pm8xxx_led_data *led, enum led_brightness value) |
| { |
| int rc; |
| u8 level; |
| |
| level = (value << PM8XXX_DRV_KEYPAD_BL_SHIFT) & |
| PM8XXX_DRV_KEYPAD_BL_MASK; |
| |
| led->reg &= ~PM8XXX_DRV_KEYPAD_BL_MASK; |
| led->reg |= level; |
| |
| rc = pm8xxx_writeb(led->dev->parent, SSBI_REG_ADDR_DRV_KEYPAD, |
| led->reg); |
| if (rc < 0) |
| dev_err(led->cdev.dev, |
| "can't set keypad backlight level rc=%d\n", rc); |
| } |
| |
| static void led_lc_set(struct pm8xxx_led_data *led, enum led_brightness value) |
| { |
| int rc, offset; |
| u8 level; |
| |
| level = (value << PM8XXX_DRV_LED_CTRL_SHIFT) & |
| PM8XXX_DRV_LED_CTRL_MASK; |
| |
| offset = PM8XXX_LED_OFFSET(led->id); |
| |
| led->reg &= ~PM8XXX_DRV_LED_CTRL_MASK; |
| led->reg |= level; |
| |
| rc = pm8xxx_writeb(led->dev->parent, SSBI_REG_ADDR_LED_CTRL(offset), |
| led->reg); |
| if (rc) |
| dev_err(led->cdev.dev, "can't set (%d) led value rc=%d\n", |
| led->id, rc); |
| } |
| |
| static void |
| led_flash_set(struct pm8xxx_led_data *led, enum led_brightness value) |
| { |
| int rc; |
| u8 level; |
| u16 reg_addr; |
| |
| level = (value << PM8XXX_DRV_FLASH_SHIFT) & |
| PM8XXX_DRV_FLASH_MASK; |
| |
| led->reg &= ~PM8XXX_DRV_FLASH_MASK; |
| led->reg |= level; |
| |
| if (led->id == PM8XXX_ID_FLASH_LED_0) |
| reg_addr = SSBI_REG_ADDR_FLASH_DRV0; |
| else |
| reg_addr = SSBI_REG_ADDR_FLASH_DRV1; |
| |
| rc = pm8xxx_writeb(led->dev->parent, reg_addr, led->reg); |
| if (rc < 0) |
| dev_err(led->cdev.dev, "can't set flash led%d level rc=%d\n", |
| led->id, rc); |
| } |
| |
| static void __pm8xxx_led_work(struct pm8xxx_led_data *led, |
| enum led_brightness level) |
| { |
| mutex_lock(&led->lock); |
| |
| switch (led->id) { |
| case PM8XXX_ID_LED_KB_LIGHT: |
| led_kp_set(led, level); |
| break; |
| case PM8XXX_ID_LED_0: |
| case PM8XXX_ID_LED_1: |
| case PM8XXX_ID_LED_2: |
| led_lc_set(led, level); |
| break; |
| case PM8XXX_ID_FLASH_LED_0: |
| case PM8XXX_ID_FLASH_LED_1: |
| led_flash_set(led, level); |
| break; |
| } |
| |
| mutex_unlock(&led->lock); |
| } |
| |
| static void pm8xxx_led_work(struct work_struct *work) |
| { |
| struct pm8xxx_led_data *led = container_of(work, |
| struct pm8xxx_led_data, work); |
| int level = led->cdev.brightness; |
| |
| __pm8xxx_led_work(led, level); |
| } |
| |
| static void pm8xxx_led_set(struct led_classdev *led_cdev, |
| enum led_brightness value) |
| { |
| struct pm8xxx_led_data *led; |
| |
| led = container_of(led_cdev, struct pm8xxx_led_data, cdev); |
| |
| if (value < LED_OFF || value > led->cdev.max_brightness) { |
| dev_err(led->cdev.dev, "Invalid brightness value exceeds"); |
| return; |
| } |
| |
| led->cdev.brightness = value; |
| schedule_work(&led->work); |
| } |
| |
| static int pm8xxx_set_led_mode_and_max_brightness(struct pm8xxx_led_data *led, |
| enum pm8xxx_led_modes led_mode, int max_current) |
| { |
| int rc = 0; |
| |
| switch (led->id) { |
| case PM8XXX_ID_LED_0: |
| case PM8XXX_ID_LED_1: |
| case PM8XXX_ID_LED_2: |
| led->cdev.max_brightness = max_current / |
| PM8XXX_ID_LED_CURRENT_FACTOR; |
| if (led->cdev.max_brightness > MAX_LC_LED_BRIGHTNESS) |
| led->cdev.max_brightness = MAX_LC_LED_BRIGHTNESS; |
| led->reg = led_mode; |
| break; |
| case PM8XXX_ID_LED_KB_LIGHT: |
| case PM8XXX_ID_FLASH_LED_0: |
| case PM8XXX_ID_FLASH_LED_1: |
| led->cdev.max_brightness = max_current / |
| PM8XXX_ID_FLASH_CURRENT_FACTOR; |
| if (led->cdev.max_brightness > MAX_FLASH_BRIGHTNESS) |
| led->cdev.max_brightness = MAX_FLASH_BRIGHTNESS; |
| |
| switch (led_mode) { |
| case PM8XXX_LED_MODE_PWM1: |
| case PM8XXX_LED_MODE_PWM2: |
| case PM8XXX_LED_MODE_PWM3: |
| led->reg = PM8XXX_FLASH_MODE_PWM; |
| break; |
| case PM8XXX_LED_MODE_DTEST1: |
| led->reg = PM8XXX_FLASH_MODE_DBUS1; |
| break; |
| case PM8XXX_LED_MODE_DTEST2: |
| led->reg = PM8XXX_FLASH_MODE_DBUS2; |
| break; |
| default: |
| led->reg = PM8XXX_LED_MODE_MANUAL; |
| break; |
| } |
| break; |
| default: |
| rc = -EINVAL; |
| pr_err("LED Id is invalid"); |
| break; |
| } |
| |
| return rc; |
| } |
| |
| static enum led_brightness pm8xxx_led_get(struct led_classdev *led_cdev) |
| { |
| struct pm8xxx_led_data *led; |
| |
| led = container_of(led_cdev, struct pm8xxx_led_data, cdev); |
| |
| return led->cdev.brightness; |
| } |
| |
| static int __devinit get_init_value(struct pm8xxx_led_data *led, u8 *val) |
| { |
| int rc, offset; |
| u16 addr; |
| |
| switch (led->id) { |
| case PM8XXX_ID_LED_KB_LIGHT: |
| addr = SSBI_REG_ADDR_DRV_KEYPAD; |
| break; |
| case PM8XXX_ID_LED_0: |
| case PM8XXX_ID_LED_1: |
| case PM8XXX_ID_LED_2: |
| offset = PM8XXX_LED_OFFSET(led->id); |
| addr = SSBI_REG_ADDR_LED_CTRL(offset); |
| break; |
| case PM8XXX_ID_FLASH_LED_0: |
| addr = SSBI_REG_ADDR_FLASH_DRV0; |
| break; |
| case PM8XXX_ID_FLASH_LED_1: |
| addr = SSBI_REG_ADDR_FLASH_DRV1; |
| break; |
| } |
| |
| rc = pm8xxx_readb(led->dev->parent, addr, val); |
| if (rc) |
| dev_err(led->cdev.dev, "can't get led(%d) level rc=%d\n", |
| led->id, rc); |
| |
| return rc; |
| } |
| |
| static int __devinit pm8xxx_led_probe(struct platform_device *pdev) |
| { |
| const struct pm8xxx_led_platform_data *pdata = pdev->dev.platform_data; |
| const struct led_platform_data *pcore_data; |
| struct led_info *curr_led; |
| struct pm8xxx_led_data *led, *led_dat; |
| struct pm8xxx_led_config *led_cfg; |
| int rc, i; |
| |
| if (pdata == NULL) { |
| dev_err(&pdev->dev, "platform data not supplied\n"); |
| return -EINVAL; |
| } |
| |
| pcore_data = pdata->led_core; |
| |
| if (pcore_data->num_leds != pdata->num_configs) { |
| dev_err(&pdev->dev, "#no. of led configs and #no. of led" |
| "entries are not equal\n"); |
| return -EINVAL; |
| } |
| |
| led = kcalloc(pcore_data->num_leds, sizeof(*led), GFP_KERNEL); |
| if (led == NULL) { |
| dev_err(&pdev->dev, "failed to alloc memory\n"); |
| return -ENOMEM; |
| } |
| |
| for (i = 0; i < pcore_data->num_leds; i++) { |
| curr_led = &pcore_data->leds[i]; |
| led_dat = &led[i]; |
| led_cfg = &pdata->configs[i]; |
| |
| led_dat->id = led_cfg->id; |
| |
| if (!((led_dat->id >= PM8XXX_ID_LED_KB_LIGHT) && |
| (led_dat->id <= PM8XXX_ID_FLASH_LED_1))) { |
| dev_err(&pdev->dev, "invalid LED ID (%d) specified\n", |
| led_dat->id); |
| rc = -EINVAL; |
| goto fail_id_check; |
| } |
| |
| led_dat->cdev.name = curr_led->name; |
| led_dat->cdev.default_trigger = curr_led->default_trigger; |
| led_dat->cdev.brightness_set = pm8xxx_led_set; |
| led_dat->cdev.brightness_get = pm8xxx_led_get; |
| led_dat->cdev.brightness = LED_OFF; |
| led_dat->cdev.flags = curr_led->flags; |
| led_dat->dev = &pdev->dev; |
| |
| rc = get_init_value(led_dat, &led_dat->reg); |
| if (rc < 0) |
| goto fail_id_check; |
| |
| rc = pm8xxx_set_led_mode_and_max_brightness(led_dat, |
| led_cfg->mode, led_cfg->max_current); |
| if (rc < 0) |
| goto fail_id_check; |
| |
| mutex_init(&led_dat->lock); |
| INIT_WORK(&led_dat->work, pm8xxx_led_work); |
| |
| rc = led_classdev_register(&pdev->dev, &led_dat->cdev); |
| if (rc) { |
| dev_err(&pdev->dev, "unable to register led %d,rc=%d\n", |
| led_dat->id, rc); |
| goto fail_id_check; |
| } |
| |
| if (led_cfg->mode != PM8XXX_LED_MODE_MANUAL) |
| __pm8xxx_led_work(led_dat, |
| led_dat->cdev.max_brightness); |
| else |
| __pm8xxx_led_work(led_dat, LED_OFF); |
| } |
| |
| platform_set_drvdata(pdev, led); |
| |
| return 0; |
| |
| fail_id_check: |
| if (i > 0) { |
| for (i = i - 1; i >= 0; i--) { |
| mutex_destroy(&led[i].lock); |
| led_classdev_unregister(&led[i].cdev); |
| } |
| } |
| kfree(led); |
| return rc; |
| } |
| |
| static int __devexit pm8xxx_led_remove(struct platform_device *pdev) |
| { |
| int i; |
| const struct led_platform_data *pdata = |
| pdev->dev.platform_data; |
| struct pm8xxx_led_data *led = platform_get_drvdata(pdev); |
| |
| for (i = 0; i < pdata->num_leds; i++) { |
| cancel_work_sync(&led[i].work); |
| mutex_destroy(&led[i].lock); |
| led_classdev_unregister(&led[i].cdev); |
| } |
| |
| kfree(led); |
| |
| return 0; |
| } |
| |
| static struct platform_driver pm8xxx_led_driver = { |
| .probe = pm8xxx_led_probe, |
| .remove = __devexit_p(pm8xxx_led_remove), |
| .driver = { |
| .name = PM8XXX_LEDS_DEV_NAME, |
| .owner = THIS_MODULE, |
| }, |
| }; |
| |
| static int __init pm8xxx_led_init(void) |
| { |
| return platform_driver_register(&pm8xxx_led_driver); |
| } |
| subsys_initcall(pm8xxx_led_init); |
| |
| static void __exit pm8xxx_led_exit(void) |
| { |
| platform_driver_unregister(&pm8xxx_led_driver); |
| } |
| module_exit(pm8xxx_led_exit); |
| |
| MODULE_DESCRIPTION("PM8XXX LEDs driver"); |
| MODULE_LICENSE("GPL v2"); |
| MODULE_VERSION("1.0"); |
| MODULE_ALIAS("platform:pm8xxx-led"); |