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> 17 #include <linux/init.h> 18 #include <linux/platform_device.h> 19 #include <linux/leds.h> 20 #include <linux/workqueue.h> 21 #include <linux/slab.h> 22 23 #include <linux/mfd/da9052/reg.h> 24 #include <linux/mfd/da9052/da9052.h> 25 #include <linux/mfd/da9052/pdata.h> 26 27 #define DA9052_OPENDRAIN_OUTPUT 2 28 #define DA9052_SET_HIGH_LVL_OUTPUT (1 << 3) 29 #define DA9052_MASK_UPPER_NIBBLE 0xF0 30 #define DA9052_MASK_LOWER_NIBBLE 0x0F 31 #define DA9052_NIBBLE_SHIFT 4 32 #define DA9052_MAX_BRIGHTNESS 0x5f 33 34 struct da9052_led { 35 struct led_classdev cdev; 36 struct work_struct work; 37 struct da9052 *da9052; 38 unsigned char led_index; 39 unsigned char id; 40 int brightness; 41 }; 42 43 static unsigned char led_reg[] = { 44 DA9052_LED_CONT_4_REG, 45 DA9052_LED_CONT_5_REG, 46 }; 47 48 static int da9052_set_led_brightness(struct da9052_led *led) 49 { 50 u8 val; 51 int error; 52 53 val = (led->brightness & 0x7f) | DA9052_LED_CONT_DIM; 54 55 error = da9052_reg_write(led->da9052, led_reg[led->led_index], val); 56 if (error < 0) 57 dev_err(led->da9052->dev, "Failed to set led brightness, %d\n", 58 error); 59 return error; 60 } 61 62 static void da9052_led_work(struct work_struct *work) 63 { 64 struct da9052_led *led = container_of(work, struct da9052_led, work); 65 66 da9052_set_led_brightness(led); 67 } 68 69 static void da9052_led_set(struct led_classdev *led_cdev, 70 enum led_brightness value) 71 { 72 struct da9052_led *led; 73 74 led = container_of(led_cdev, struct da9052_led, cdev); 75 led->brightness = value; 76 schedule_work(&led->work); 77 } 78 79 static int da9052_configure_leds(struct da9052 *da9052) 80 { 81 int error; 82 unsigned char register_value = DA9052_OPENDRAIN_OUTPUT 83 | DA9052_SET_HIGH_LVL_OUTPUT; 84 85 error = da9052_reg_update(da9052, DA9052_GPIO_14_15_REG, 86 DA9052_MASK_LOWER_NIBBLE, 87 register_value); 88 89 if (error < 0) { 90 dev_err(da9052->dev, "Failed to write GPIO 14-15 reg, %d\n", 91 error); 92 return error; 93 } 94 95 error = da9052_reg_update(da9052, DA9052_GPIO_14_15_REG, 96 DA9052_MASK_UPPER_NIBBLE, 97 register_value << DA9052_NIBBLE_SHIFT); 98 if (error < 0) 99 dev_err(da9052->dev, "Failed to write GPIO 14-15 reg, %d\n", 100 error); 101 102 return error; 103 } 104 105 static int da9052_led_probe(struct platform_device *pdev) 106 { 107 struct da9052_pdata *pdata; 108 struct da9052 *da9052; 109 struct led_platform_data *pled; 110 struct da9052_led *led = NULL; 111 int error = -ENODEV; 112 int i; 113 114 da9052 = dev_get_drvdata(pdev->dev.parent); 115 pdata = dev_get_platdata(da9052->dev); 116 if (pdata == NULL) { 117 dev_err(&pdev->dev, "No platform data\n"); 118 goto err; 119 } 120 121 pled = pdata->pled; 122 if (pled == NULL) { 123 dev_err(&pdev->dev, "No platform data for LED\n"); 124 goto err; 125 } 126 127 led = devm_kzalloc(&pdev->dev, 128 sizeof(struct da9052_led) * pled->num_leds, 129 GFP_KERNEL); 130 if (led == NULL) { 131 dev_err(&pdev->dev, "Failed to alloc memory\n"); 132 error = -ENOMEM; 133 goto err; 134 } 135 136 for (i = 0; i < pled->num_leds; i++) { 137 led[i].cdev.name = pled->leds[i].name; 138 led[i].cdev.brightness_set = da9052_led_set; 139 led[i].cdev.brightness = LED_OFF; 140 led[i].cdev.max_brightness = DA9052_MAX_BRIGHTNESS; 141 led[i].brightness = LED_OFF; 142 led[i].led_index = pled->leds[i].flags; 143 led[i].da9052 = dev_get_drvdata(pdev->dev.parent); 144 INIT_WORK(&led[i].work, da9052_led_work); 145 146 error = led_classdev_register(pdev->dev.parent, &led[i].cdev); 147 if (error) { 148 dev_err(&pdev->dev, "Failed to register led %d\n", 149 led[i].led_index); 150 goto err_register; 151 } 152 153 error = da9052_set_led_brightness(&led[i]); 154 if (error) { 155 dev_err(&pdev->dev, "Unable to init led %d\n", 156 led[i].led_index); 157 continue; 158 } 159 } 160 error = da9052_configure_leds(led->da9052); 161 if (error) { 162 dev_err(&pdev->dev, "Failed to configure GPIO LED%d\n", error); 163 goto err_register; 164 } 165 166 platform_set_drvdata(pdev, led); 167 168 return 0; 169 170 err_register: 171 for (i = i - 1; i >= 0; i--) { 172 led_classdev_unregister(&led[i].cdev); 173 cancel_work_sync(&led[i].work); 174 } 175 err: 176 return error; 177 } 178 179 static int da9052_led_remove(struct platform_device *pdev) 180 { 181 struct da9052_led *led = platform_get_drvdata(pdev); 182 struct da9052_pdata *pdata; 183 struct da9052 *da9052; 184 struct led_platform_data *pled; 185 int i; 186 187 da9052 = dev_get_drvdata(pdev->dev.parent); 188 pdata = dev_get_platdata(da9052->dev); 189 pled = pdata->pled; 190 191 for (i = 0; i < pled->num_leds; i++) { 192 led[i].brightness = 0; 193 da9052_set_led_brightness(&led[i]); 194 led_classdev_unregister(&led[i].cdev); 195 cancel_work_sync(&led[i].work); 196 } 197 198 return 0; 199 } 200 201 static struct platform_driver da9052_led_driver = { 202 .driver = { 203 .name = "da9052-leds", 204 .owner = THIS_MODULE, 205 }, 206 .probe = da9052_led_probe, 207 .remove = da9052_led_remove, 208 }; 209 210 module_platform_driver(da9052_led_driver); 211 212 MODULE_AUTHOR("Dialog Semiconductor Ltd <dchen@diasemi.com>"); 213 MODULE_DESCRIPTION("LED driver for Dialog DA9052 PMIC"); 214 MODULE_LICENSE("GPL"); 215