Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/leds-pwm.c |
| 3 | * |
| 4 | * simple PWM based LED control |
| 5 | * |
| 6 | * Copyright 2009 Luotao Fu @ Pengutronix (l.fu@pengutronix.de) |
| 7 | * |
| 8 | * based on leds-gpio.c by Raphael Assenat <raph@8d.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/platform_device.h> |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 19 | #include <linux/of_platform.h> |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 20 | #include <linux/fb.h> |
| 21 | #include <linux/leds.h> |
| 22 | #include <linux/err.h> |
| 23 | #include <linux/pwm.h> |
| 24 | #include <linux/leds_pwm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 26 | |
| 27 | struct led_pwm_data { |
| 28 | struct led_classdev cdev; |
| 29 | struct pwm_device *pwm; |
Sachin Kamat | dcba910 | 2012-11-25 12:24:55 +0530 | [diff] [blame] | 30 | unsigned int active_low; |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 31 | unsigned int period; |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 32 | }; |
| 33 | |
Peter Ujfalusi | 0f86815 | 2012-12-21 01:43:56 -0800 | [diff] [blame] | 34 | struct led_pwm_priv { |
| 35 | int num_leds; |
| 36 | struct led_pwm_data leds[0]; |
| 37 | }; |
| 38 | |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 39 | static void led_pwm_set(struct led_classdev *led_cdev, |
| 40 | enum led_brightness brightness) |
| 41 | { |
| 42 | struct led_pwm_data *led_dat = |
| 43 | container_of(led_cdev, struct led_pwm_data, cdev); |
Lars-Peter Clausen | e459062 | 2009-11-27 06:17:38 +0100 | [diff] [blame] | 44 | unsigned int max = led_dat->cdev.max_brightness; |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 45 | unsigned int period = led_dat->period; |
| 46 | |
| 47 | if (brightness == 0) { |
| 48 | pwm_config(led_dat->pwm, 0, period); |
| 49 | pwm_disable(led_dat->pwm); |
| 50 | } else { |
| 51 | pwm_config(led_dat->pwm, brightness * period / max, period); |
| 52 | pwm_enable(led_dat->pwm); |
| 53 | } |
| 54 | } |
| 55 | |
Peter Ujfalusi | 0f86815 | 2012-12-21 01:43:56 -0800 | [diff] [blame] | 56 | static inline size_t sizeof_pwm_leds_priv(int num_leds) |
| 57 | { |
| 58 | return sizeof(struct led_pwm_priv) + |
| 59 | (sizeof(struct led_pwm_data) * num_leds); |
| 60 | } |
| 61 | |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 62 | static struct led_pwm_priv *led_pwm_create_of(struct platform_device *pdev) |
| 63 | { |
| 64 | struct device_node *node = pdev->dev.of_node; |
| 65 | struct device_node *child; |
| 66 | struct led_pwm_priv *priv; |
| 67 | int count, ret; |
| 68 | |
| 69 | /* count LEDs in this device, so we know how much to allocate */ |
| 70 | count = of_get_child_count(node); |
| 71 | if (!count) |
| 72 | return NULL; |
| 73 | |
| 74 | priv = devm_kzalloc(&pdev->dev, sizeof_pwm_leds_priv(count), |
| 75 | GFP_KERNEL); |
| 76 | if (!priv) |
| 77 | return NULL; |
| 78 | |
| 79 | for_each_child_of_node(node, child) { |
| 80 | struct led_pwm_data *led_dat = &priv->leds[priv->num_leds]; |
| 81 | |
| 82 | led_dat->cdev.name = of_get_property(child, "label", |
| 83 | NULL) ? : child->name; |
| 84 | |
| 85 | led_dat->pwm = devm_of_pwm_get(&pdev->dev, child, NULL); |
| 86 | if (IS_ERR(led_dat->pwm)) { |
| 87 | dev_err(&pdev->dev, "unable to request PWM for %s\n", |
| 88 | led_dat->cdev.name); |
| 89 | goto err; |
| 90 | } |
| 91 | /* Get the period from PWM core when n*/ |
| 92 | led_dat->period = pwm_get_period(led_dat->pwm); |
| 93 | |
| 94 | led_dat->cdev.default_trigger = of_get_property(child, |
| 95 | "linux,default-trigger", NULL); |
| 96 | of_property_read_u32(child, "max-brightness", |
| 97 | &led_dat->cdev.max_brightness); |
| 98 | |
| 99 | led_dat->cdev.brightness_set = led_pwm_set; |
| 100 | led_dat->cdev.brightness = LED_OFF; |
| 101 | led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME; |
| 102 | |
| 103 | ret = led_classdev_register(&pdev->dev, &led_dat->cdev); |
| 104 | if (ret < 0) { |
| 105 | dev_err(&pdev->dev, "failed to register for %s\n", |
| 106 | led_dat->cdev.name); |
| 107 | of_node_put(child); |
| 108 | goto err; |
| 109 | } |
| 110 | priv->num_leds++; |
| 111 | } |
| 112 | |
| 113 | return priv; |
| 114 | err: |
| 115 | while (priv->num_leds--) |
| 116 | led_classdev_unregister(&priv->leds[priv->num_leds].cdev); |
| 117 | |
| 118 | return NULL; |
| 119 | } |
| 120 | |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 121 | static int led_pwm_probe(struct platform_device *pdev) |
| 122 | { |
| 123 | struct led_pwm_platform_data *pdata = pdev->dev.platform_data; |
Peter Ujfalusi | 0f86815 | 2012-12-21 01:43:56 -0800 | [diff] [blame] | 124 | struct led_pwm_priv *priv; |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 125 | int i, ret = 0; |
| 126 | |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 127 | if (pdata && pdata->num_leds) { |
| 128 | priv = devm_kzalloc(&pdev->dev, |
| 129 | sizeof_pwm_leds_priv(pdata->num_leds), |
| 130 | GFP_KERNEL); |
| 131 | if (!priv) |
| 132 | return -ENOMEM; |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 133 | |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 134 | for (i = 0; i < pdata->num_leds; i++) { |
| 135 | struct led_pwm *cur_led = &pdata->leds[i]; |
| 136 | struct led_pwm_data *led_dat = &priv->leds[i]; |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 137 | |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 138 | led_dat->pwm = devm_pwm_get(&pdev->dev, cur_led->name); |
| 139 | if (IS_ERR(led_dat->pwm)) { |
| 140 | ret = PTR_ERR(led_dat->pwm); |
| 141 | dev_err(&pdev->dev, |
| 142 | "unable to request PWM for %s\n", |
| 143 | cur_led->name); |
| 144 | goto err; |
| 145 | } |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 146 | |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 147 | led_dat->cdev.name = cur_led->name; |
| 148 | led_dat->cdev.default_trigger = cur_led->default_trigger; |
| 149 | led_dat->active_low = cur_led->active_low; |
| 150 | led_dat->period = cur_led->pwm_period_ns; |
| 151 | led_dat->cdev.brightness_set = led_pwm_set; |
| 152 | led_dat->cdev.brightness = LED_OFF; |
| 153 | led_dat->cdev.max_brightness = cur_led->max_brightness; |
| 154 | led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME; |
| 155 | |
| 156 | ret = led_classdev_register(&pdev->dev, &led_dat->cdev); |
| 157 | if (ret < 0) |
| 158 | goto err; |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 159 | } |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 160 | priv->num_leds = pdata->num_leds; |
| 161 | } else { |
| 162 | priv = led_pwm_create_of(pdev); |
| 163 | if (!priv) |
| 164 | return -ENODEV; |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Peter Ujfalusi | 0f86815 | 2012-12-21 01:43:56 -0800 | [diff] [blame] | 167 | platform_set_drvdata(pdev, priv); |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 168 | |
| 169 | return 0; |
| 170 | |
| 171 | err: |
Peter Ujfalusi | 8a66a57 | 2012-12-21 01:44:00 -0800 | [diff] [blame] | 172 | while (i--) |
| 173 | led_classdev_unregister(&priv->leds[i].cdev); |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 174 | |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 175 | return ret; |
| 176 | } |
| 177 | |
Bill Pemberton | 678e8a6 | 2012-11-19 13:26:00 -0500 | [diff] [blame] | 178 | static int led_pwm_remove(struct platform_device *pdev) |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 179 | { |
Peter Ujfalusi | 0f86815 | 2012-12-21 01:43:56 -0800 | [diff] [blame] | 180 | struct led_pwm_priv *priv = platform_get_drvdata(pdev); |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 181 | int i; |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 182 | |
Peter Ujfalusi | 0f86815 | 2012-12-21 01:43:56 -0800 | [diff] [blame] | 183 | for (i = 0; i < priv->num_leds; i++) |
| 184 | led_classdev_unregister(&priv->leds[i].cdev); |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 185 | |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 189 | static const struct of_device_id of_pwm_leds_match[] = { |
| 190 | { .compatible = "pwm-leds", }, |
| 191 | {}, |
| 192 | }; |
| 193 | MODULE_DEVICE_TABLE(of, of_pwm_leds_match); |
| 194 | |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 195 | static struct platform_driver led_pwm_driver = { |
| 196 | .probe = led_pwm_probe, |
Bill Pemberton | df07cf8 | 2012-11-19 13:20:20 -0500 | [diff] [blame] | 197 | .remove = led_pwm_remove, |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 198 | .driver = { |
| 199 | .name = "leds_pwm", |
| 200 | .owner = THIS_MODULE, |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 201 | .of_match_table = of_match_ptr(of_pwm_leds_match), |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 202 | }, |
| 203 | }; |
| 204 | |
Axel Lin | 892a884 | 2012-01-10 15:09:24 -0800 | [diff] [blame] | 205 | module_platform_driver(led_pwm_driver); |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 206 | |
| 207 | MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>"); |
| 208 | MODULE_DESCRIPTION("PWM LED driver for PXA"); |
| 209 | MODULE_LICENSE("GPL"); |
| 210 | MODULE_ALIAS("platform:leds-pwm"); |