David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * LED Driver for Dialog DA9052 PMICs. |
| 3 | * |
| 4 | * Copyright(c) 2012 Dialog Semiconductor Ltd. |
| 5 | * |
| 6 | * Author: David Dajun Chen <dchen@diasemi.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/kernel.h> |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/leds.h> |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 19 | #include <linux/slab.h> |
| 20 | |
| 21 | #include <linux/mfd/da9052/reg.h> |
| 22 | #include <linux/mfd/da9052/da9052.h> |
| 23 | #include <linux/mfd/da9052/pdata.h> |
| 24 | |
| 25 | #define DA9052_OPENDRAIN_OUTPUT 2 |
| 26 | #define DA9052_SET_HIGH_LVL_OUTPUT (1 << 3) |
| 27 | #define DA9052_MASK_UPPER_NIBBLE 0xF0 |
| 28 | #define DA9052_MASK_LOWER_NIBBLE 0x0F |
| 29 | #define DA9052_NIBBLE_SHIFT 4 |
| 30 | #define DA9052_MAX_BRIGHTNESS 0x5f |
| 31 | |
| 32 | struct da9052_led { |
| 33 | struct led_classdev cdev; |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 34 | struct da9052 *da9052; |
| 35 | unsigned char led_index; |
| 36 | unsigned char id; |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | static unsigned char led_reg[] = { |
| 40 | DA9052_LED_CONT_4_REG, |
| 41 | DA9052_LED_CONT_5_REG, |
| 42 | }; |
| 43 | |
Andrew Lunn | 0a1f01d | 2015-08-20 12:45:23 +0200 | [diff] [blame] | 44 | static int da9052_set_led_brightness(struct da9052_led *led, |
| 45 | enum led_brightness brightness) |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 46 | { |
| 47 | u8 val; |
| 48 | int error; |
| 49 | |
Andrew Lunn | 0a1f01d | 2015-08-20 12:45:23 +0200 | [diff] [blame] | 50 | val = (brightness & 0x7f) | DA9052_LED_CONT_DIM; |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 51 | |
| 52 | error = da9052_reg_write(led->da9052, led_reg[led->led_index], val); |
| 53 | if (error < 0) |
| 54 | dev_err(led->da9052->dev, "Failed to set led brightness, %d\n", |
| 55 | error); |
| 56 | return error; |
| 57 | } |
| 58 | |
Andrew Lunn | 0a1f01d | 2015-08-20 12:45:23 +0200 | [diff] [blame] | 59 | static int da9052_led_set(struct led_classdev *led_cdev, |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 60 | enum led_brightness value) |
| 61 | { |
Andrew Lunn | 0a1f01d | 2015-08-20 12:45:23 +0200 | [diff] [blame] | 62 | struct da9052_led *led = |
| 63 | container_of(led_cdev, struct da9052_led, cdev); |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 64 | |
Andrew Lunn | 0a1f01d | 2015-08-20 12:45:23 +0200 | [diff] [blame] | 65 | return da9052_set_led_brightness(led, value); |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static int da9052_configure_leds(struct da9052 *da9052) |
| 69 | { |
| 70 | int error; |
| 71 | unsigned char register_value = DA9052_OPENDRAIN_OUTPUT |
| 72 | | DA9052_SET_HIGH_LVL_OUTPUT; |
| 73 | |
| 74 | error = da9052_reg_update(da9052, DA9052_GPIO_14_15_REG, |
| 75 | DA9052_MASK_LOWER_NIBBLE, |
| 76 | register_value); |
| 77 | |
| 78 | if (error < 0) { |
| 79 | dev_err(da9052->dev, "Failed to write GPIO 14-15 reg, %d\n", |
| 80 | error); |
| 81 | return error; |
| 82 | } |
| 83 | |
| 84 | error = da9052_reg_update(da9052, DA9052_GPIO_14_15_REG, |
| 85 | DA9052_MASK_UPPER_NIBBLE, |
| 86 | register_value << DA9052_NIBBLE_SHIFT); |
| 87 | if (error < 0) |
| 88 | dev_err(da9052->dev, "Failed to write GPIO 14-15 reg, %d\n", |
| 89 | error); |
| 90 | |
| 91 | return error; |
| 92 | } |
| 93 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 94 | static int da9052_led_probe(struct platform_device *pdev) |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 95 | { |
| 96 | struct da9052_pdata *pdata; |
| 97 | struct da9052 *da9052; |
| 98 | struct led_platform_data *pled; |
| 99 | struct da9052_led *led = NULL; |
| 100 | int error = -ENODEV; |
| 101 | int i; |
| 102 | |
| 103 | da9052 = dev_get_drvdata(pdev->dev.parent); |
Jingoo Han | 87aae1e | 2013-07-30 01:07:35 -0700 | [diff] [blame] | 104 | pdata = dev_get_platdata(da9052->dev); |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 105 | if (pdata == NULL) { |
| 106 | dev_err(&pdev->dev, "No platform data\n"); |
| 107 | goto err; |
| 108 | } |
| 109 | |
| 110 | pled = pdata->pled; |
| 111 | if (pled == NULL) { |
| 112 | dev_err(&pdev->dev, "No platform data for LED\n"); |
| 113 | goto err; |
| 114 | } |
| 115 | |
| 116 | led = devm_kzalloc(&pdev->dev, |
| 117 | sizeof(struct da9052_led) * pled->num_leds, |
| 118 | GFP_KERNEL); |
Xiubo Li | 0c9a03b | 2014-05-07 20:25:52 -0700 | [diff] [blame] | 119 | if (!led) { |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 120 | error = -ENOMEM; |
| 121 | goto err; |
| 122 | } |
| 123 | |
| 124 | for (i = 0; i < pled->num_leds; i++) { |
| 125 | led[i].cdev.name = pled->leds[i].name; |
Andrew Lunn | 0a1f01d | 2015-08-20 12:45:23 +0200 | [diff] [blame] | 126 | led[i].cdev.brightness_set_blocking = da9052_led_set; |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 127 | led[i].cdev.brightness = LED_OFF; |
| 128 | led[i].cdev.max_brightness = DA9052_MAX_BRIGHTNESS; |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 129 | led[i].led_index = pled->leds[i].flags; |
| 130 | led[i].da9052 = dev_get_drvdata(pdev->dev.parent); |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 131 | |
| 132 | error = led_classdev_register(pdev->dev.parent, &led[i].cdev); |
| 133 | if (error) { |
| 134 | dev_err(&pdev->dev, "Failed to register led %d\n", |
| 135 | led[i].led_index); |
| 136 | goto err_register; |
| 137 | } |
| 138 | |
Andrew Lunn | 0a1f01d | 2015-08-20 12:45:23 +0200 | [diff] [blame] | 139 | error = da9052_set_led_brightness(&led[i], |
| 140 | led[i].cdev.brightness); |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 141 | if (error) { |
| 142 | dev_err(&pdev->dev, "Unable to init led %d\n", |
| 143 | led[i].led_index); |
| 144 | continue; |
| 145 | } |
| 146 | } |
| 147 | error = da9052_configure_leds(led->da9052); |
| 148 | if (error) { |
| 149 | dev_err(&pdev->dev, "Failed to configure GPIO LED%d\n", error); |
| 150 | goto err_register; |
| 151 | } |
| 152 | |
| 153 | platform_set_drvdata(pdev, led); |
| 154 | |
| 155 | return 0; |
| 156 | |
| 157 | err_register: |
Andrew Lunn | 0a1f01d | 2015-08-20 12:45:23 +0200 | [diff] [blame] | 158 | for (i = i - 1; i >= 0; i--) |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 159 | led_classdev_unregister(&led[i].cdev); |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 160 | err: |
| 161 | return error; |
| 162 | } |
| 163 | |
Bill Pemberton | 678e8a6 | 2012-11-19 13:26:00 -0500 | [diff] [blame] | 164 | static int da9052_led_remove(struct platform_device *pdev) |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 165 | { |
| 166 | struct da9052_led *led = platform_get_drvdata(pdev); |
| 167 | struct da9052_pdata *pdata; |
| 168 | struct da9052 *da9052; |
| 169 | struct led_platform_data *pled; |
| 170 | int i; |
| 171 | |
| 172 | da9052 = dev_get_drvdata(pdev->dev.parent); |
Jingoo Han | 87aae1e | 2013-07-30 01:07:35 -0700 | [diff] [blame] | 173 | pdata = dev_get_platdata(da9052->dev); |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 174 | pled = pdata->pled; |
| 175 | |
| 176 | for (i = 0; i < pled->num_leds; i++) { |
Andrew Lunn | 0a1f01d | 2015-08-20 12:45:23 +0200 | [diff] [blame] | 177 | da9052_set_led_brightness(&led[i], LED_OFF); |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 178 | led_classdev_unregister(&led[i].cdev); |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static struct platform_driver da9052_led_driver = { |
| 185 | .driver = { |
| 186 | .name = "da9052-leds", |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 187 | }, |
| 188 | .probe = da9052_led_probe, |
Bill Pemberton | df07cf8 | 2012-11-19 13:20:20 -0500 | [diff] [blame] | 189 | .remove = da9052_led_remove, |
David Dajun Chen | 5ba7363 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | module_platform_driver(da9052_led_driver); |
| 193 | |
| 194 | MODULE_AUTHOR("Dialog Semiconductor Ltd <dchen@diasemi.com>"); |
| 195 | MODULE_DESCRIPTION("LED driver for Dialog DA9052 PMIC"); |
| 196 | MODULE_LICENSE("GPL"); |