Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 1 | /* |
| 2 | * TI LP8788 MFD - keyled driver |
| 3 | * |
| 4 | * Copyright 2012 Texas Instruments |
| 5 | * |
| 6 | * Author: Milo(Woogyom) Kim <milo.kim@ti.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/leds.h> |
| 19 | #include <linux/mutex.h> |
| 20 | #include <linux/mfd/lp8788.h> |
| 21 | #include <linux/mfd/lp8788-isink.h> |
| 22 | |
| 23 | #define MAX_BRIGHTNESS LP8788_ISINK_MAX_PWM |
| 24 | #define DEFAULT_LED_NAME "keyboard-backlight" |
| 25 | |
| 26 | struct lp8788_led { |
| 27 | struct lp8788 *lp; |
| 28 | struct mutex lock; |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 29 | struct led_classdev led_dev; |
| 30 | enum lp8788_isink_number isink_num; |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 31 | int on; |
| 32 | }; |
| 33 | |
| 34 | struct lp8788_led_config { |
| 35 | enum lp8788_isink_scale scale; |
| 36 | enum lp8788_isink_number num; |
| 37 | int iout; |
| 38 | }; |
| 39 | |
| 40 | static struct lp8788_led_config default_led_config = { |
| 41 | .scale = LP8788_ISINK_SCALE_100mA, |
| 42 | .num = LP8788_ISINK_3, |
| 43 | .iout = 0, |
| 44 | }; |
| 45 | |
| 46 | static int lp8788_led_init_device(struct lp8788_led *led, |
| 47 | struct lp8788_led_platform_data *pdata) |
| 48 | { |
| 49 | struct lp8788_led_config *cfg = &default_led_config; |
| 50 | u8 addr, mask, val; |
| 51 | int ret; |
| 52 | |
| 53 | if (pdata) { |
| 54 | cfg->scale = pdata->scale; |
| 55 | cfg->num = pdata->num; |
| 56 | cfg->iout = pdata->iout_code; |
| 57 | } |
| 58 | |
| 59 | led->isink_num = cfg->num; |
| 60 | |
| 61 | /* scale configuration */ |
| 62 | addr = LP8788_ISINK_CTRL; |
| 63 | mask = 1 << (cfg->num + LP8788_ISINK_SCALE_OFFSET); |
Axel Lin | 4df7309 | 2012-08-01 20:40:34 +0800 | [diff] [blame] | 64 | val = cfg->scale << (cfg->num + LP8788_ISINK_SCALE_OFFSET); |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 65 | ret = lp8788_update_bits(led->lp, addr, mask, val); |
| 66 | if (ret) |
| 67 | return ret; |
| 68 | |
| 69 | /* current configuration */ |
| 70 | addr = lp8788_iout_addr[cfg->num]; |
| 71 | mask = lp8788_iout_mask[cfg->num]; |
| 72 | val = cfg->iout; |
| 73 | |
| 74 | return lp8788_update_bits(led->lp, addr, mask, val); |
| 75 | } |
| 76 | |
Andrew Lunn | 6499837 | 2015-08-20 12:27:05 +0200 | [diff] [blame] | 77 | static int lp8788_led_enable(struct lp8788_led *led, |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 78 | enum lp8788_isink_number num, int on) |
| 79 | { |
Andrew Lunn | 6499837 | 2015-08-20 12:27:05 +0200 | [diff] [blame] | 80 | int ret; |
| 81 | |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 82 | u8 mask = 1 << num; |
| 83 | u8 val = on << num; |
| 84 | |
Andrew Lunn | 6499837 | 2015-08-20 12:27:05 +0200 | [diff] [blame] | 85 | ret = lp8788_update_bits(led->lp, LP8788_ISINK_CTRL, mask, val); |
| 86 | if (ret == 0) |
| 87 | led->on = on; |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 88 | |
Andrew Lunn | 6499837 | 2015-08-20 12:27:05 +0200 | [diff] [blame] | 89 | return ret; |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 90 | } |
| 91 | |
Andrew Lunn | 6499837 | 2015-08-20 12:27:05 +0200 | [diff] [blame] | 92 | static int lp8788_brightness_set(struct led_classdev *led_cdev, |
| 93 | enum led_brightness val) |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 94 | { |
Andrew Lunn | 6499837 | 2015-08-20 12:27:05 +0200 | [diff] [blame] | 95 | struct lp8788_led *led = |
| 96 | container_of(led_cdev, struct lp8788_led, led_dev); |
| 97 | |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 98 | enum lp8788_isink_number num = led->isink_num; |
Andrew Lunn | 6499837 | 2015-08-20 12:27:05 +0200 | [diff] [blame] | 99 | int enable, ret; |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 100 | |
| 101 | mutex_lock(&led->lock); |
| 102 | |
| 103 | switch (num) { |
| 104 | case LP8788_ISINK_1: |
| 105 | case LP8788_ISINK_2: |
| 106 | case LP8788_ISINK_3: |
Andrew Lunn | 6499837 | 2015-08-20 12:27:05 +0200 | [diff] [blame] | 107 | ret = lp8788_write_byte(led->lp, lp8788_pwm_addr[num], val); |
| 108 | if (ret < 0) |
| 109 | goto unlock; |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 110 | break; |
| 111 | default: |
Devendra Naga | d45bb11 | 2012-07-27 02:00:31 +0800 | [diff] [blame] | 112 | mutex_unlock(&led->lock); |
Andrew Lunn | 6499837 | 2015-08-20 12:27:05 +0200 | [diff] [blame] | 113 | return -EINVAL; |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | enable = (val > 0) ? 1 : 0; |
| 117 | if (enable != led->on) |
Andrew Lunn | 6499837 | 2015-08-20 12:27:05 +0200 | [diff] [blame] | 118 | ret = lp8788_led_enable(led, num, enable); |
| 119 | unlock: |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 120 | mutex_unlock(&led->lock); |
Andrew Lunn | 6499837 | 2015-08-20 12:27:05 +0200 | [diff] [blame] | 121 | return ret; |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 122 | } |
| 123 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 124 | static int lp8788_led_probe(struct platform_device *pdev) |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 125 | { |
| 126 | struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent); |
| 127 | struct lp8788_led_platform_data *led_pdata; |
| 128 | struct lp8788_led *led; |
Kim, Milo | a1932ed | 2013-01-02 22:28:12 -0800 | [diff] [blame] | 129 | struct device *dev = &pdev->dev; |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 130 | int ret; |
| 131 | |
Kim, Milo | a1932ed | 2013-01-02 22:28:12 -0800 | [diff] [blame] | 132 | led = devm_kzalloc(dev, sizeof(struct lp8788_led), GFP_KERNEL); |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 133 | if (!led) |
| 134 | return -ENOMEM; |
| 135 | |
| 136 | led->lp = lp; |
| 137 | led->led_dev.max_brightness = MAX_BRIGHTNESS; |
Andrew Lunn | 6499837 | 2015-08-20 12:27:05 +0200 | [diff] [blame] | 138 | led->led_dev.brightness_set_blocking = lp8788_brightness_set; |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 139 | |
| 140 | led_pdata = lp->pdata ? lp->pdata->led_pdata : NULL; |
| 141 | |
| 142 | if (!led_pdata || !led_pdata->name) |
| 143 | led->led_dev.name = DEFAULT_LED_NAME; |
| 144 | else |
| 145 | led->led_dev.name = led_pdata->name; |
| 146 | |
| 147 | mutex_init(&led->lock); |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 148 | |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 149 | ret = lp8788_led_init_device(led, led_pdata); |
| 150 | if (ret) { |
Kim, Milo | a1932ed | 2013-01-02 22:28:12 -0800 | [diff] [blame] | 151 | dev_err(dev, "led init device err: %d\n", ret); |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 152 | return ret; |
| 153 | } |
| 154 | |
Amitoj Kaur Chawla | f165a66 | 2016-03-09 08:53:38 +0530 | [diff] [blame] | 155 | ret = devm_led_classdev_register(dev, &led->led_dev); |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 156 | if (ret) { |
Kim, Milo | a1932ed | 2013-01-02 22:28:12 -0800 | [diff] [blame] | 157 | dev_err(dev, "led register err: %d\n", ret); |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 164 | static struct platform_driver lp8788_led_driver = { |
| 165 | .probe = lp8788_led_probe, |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 166 | .driver = { |
| 167 | .name = LP8788_DEV_KEYLED, |
Kim, Milo | eb80411 | 2012-07-20 16:43:59 +0800 | [diff] [blame] | 168 | }, |
| 169 | }; |
| 170 | module_platform_driver(lp8788_led_driver); |
| 171 | |
| 172 | MODULE_DESCRIPTION("Texas Instruments LP8788 Keyboard LED Driver"); |
| 173 | MODULE_AUTHOR("Milo Kim"); |
| 174 | MODULE_LICENSE("GPL"); |
| 175 | MODULE_ALIAS("platform:lp8788-keyled"); |