0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/err.h>
0009 #include <linux/kernel.h>
0010 #include <linux/led-class-multicolor.h>
0011 #include <linux/leds.h>
0012 #include <linux/mod_devicetable.h>
0013 #include <linux/module.h>
0014 #include <linux/mutex.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/property.h>
0017 #include <linux/pwm.h>
0018
0019 struct pwm_led {
0020 struct pwm_device *pwm;
0021 struct pwm_state state;
0022 bool active_low;
0023 };
0024
0025 struct pwm_mc_led {
0026 struct led_classdev_mc mc_cdev;
0027 struct mutex lock;
0028 struct pwm_led leds[];
0029 };
0030
0031 static int led_pwm_mc_set(struct led_classdev *cdev,
0032 enum led_brightness brightness)
0033 {
0034 struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev);
0035 struct pwm_mc_led *priv = container_of(mc_cdev, struct pwm_mc_led, mc_cdev);
0036 unsigned long long duty;
0037 int ret = 0;
0038 int i;
0039
0040 led_mc_calc_color_components(mc_cdev, brightness);
0041
0042 mutex_lock(&priv->lock);
0043
0044 for (i = 0; i < mc_cdev->num_colors; i++) {
0045 duty = priv->leds[i].state.period;
0046 duty *= mc_cdev->subled_info[i].brightness;
0047 do_div(duty, cdev->max_brightness);
0048
0049 if (priv->leds[i].active_low)
0050 duty = priv->leds[i].state.period - duty;
0051
0052 priv->leds[i].state.duty_cycle = duty;
0053 priv->leds[i].state.enabled = duty > 0;
0054 ret = pwm_apply_state(priv->leds[i].pwm,
0055 &priv->leds[i].state);
0056 if (ret)
0057 break;
0058 }
0059
0060 mutex_unlock(&priv->lock);
0061
0062 return ret;
0063 }
0064
0065 static int iterate_subleds(struct device *dev, struct pwm_mc_led *priv,
0066 struct fwnode_handle *mcnode)
0067 {
0068 struct mc_subled *subled = priv->mc_cdev.subled_info;
0069 struct fwnode_handle *fwnode;
0070 struct pwm_led *pwmled;
0071 u32 color;
0072 int ret;
0073
0074
0075 fwnode_for_each_child_node(mcnode, fwnode) {
0076 pwmled = &priv->leds[priv->mc_cdev.num_colors];
0077 pwmled->pwm = devm_fwnode_pwm_get(dev, fwnode, NULL);
0078 if (IS_ERR(pwmled->pwm)) {
0079 ret = dev_err_probe(dev, PTR_ERR(pwmled->pwm), "unable to request PWM\n");
0080 goto release_fwnode;
0081 }
0082 pwm_init_state(pwmled->pwm, &pwmled->state);
0083 pwmled->active_low = fwnode_property_read_bool(fwnode, "active-low");
0084
0085 ret = fwnode_property_read_u32(fwnode, "color", &color);
0086 if (ret) {
0087 dev_err(dev, "cannot read color: %d\n", ret);
0088 goto release_fwnode;
0089 }
0090
0091 subled[priv->mc_cdev.num_colors].color_index = color;
0092 priv->mc_cdev.num_colors++;
0093 }
0094
0095 return 0;
0096
0097 release_fwnode:
0098 fwnode_handle_put(fwnode);
0099 return ret;
0100 }
0101
0102 static int led_pwm_mc_probe(struct platform_device *pdev)
0103 {
0104 struct fwnode_handle *mcnode, *fwnode;
0105 struct led_init_data init_data = {};
0106 struct led_classdev *cdev;
0107 struct mc_subled *subled;
0108 struct pwm_mc_led *priv;
0109 int count = 0;
0110 int ret = 0;
0111
0112 mcnode = device_get_named_child_node(&pdev->dev, "multi-led");
0113 if (!mcnode)
0114 return dev_err_probe(&pdev->dev, -ENODEV,
0115 "expected multi-led node\n");
0116
0117
0118 fwnode_for_each_child_node(mcnode, fwnode)
0119 count++;
0120
0121 priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, count),
0122 GFP_KERNEL);
0123 if (!priv) {
0124 ret = -ENOMEM;
0125 goto release_mcnode;
0126 }
0127 mutex_init(&priv->lock);
0128
0129 subled = devm_kcalloc(&pdev->dev, count, sizeof(*subled), GFP_KERNEL);
0130 if (!subled) {
0131 ret = -ENOMEM;
0132 goto release_mcnode;
0133 }
0134 priv->mc_cdev.subled_info = subled;
0135
0136
0137 cdev = &priv->mc_cdev.led_cdev;
0138 fwnode_property_read_u32(mcnode, "max-brightness",
0139 &cdev->max_brightness);
0140 cdev->flags = LED_CORE_SUSPENDRESUME;
0141 cdev->brightness_set_blocking = led_pwm_mc_set;
0142
0143 ret = iterate_subleds(&pdev->dev, priv, mcnode);
0144 if (ret)
0145 goto release_mcnode;
0146
0147 init_data.fwnode = mcnode;
0148 ret = devm_led_classdev_multicolor_register_ext(&pdev->dev,
0149 &priv->mc_cdev,
0150 &init_data);
0151 if (ret) {
0152 dev_err(&pdev->dev,
0153 "failed to register multicolor PWM led for %s: %d\n",
0154 cdev->name, ret);
0155 goto release_mcnode;
0156 }
0157
0158 ret = led_pwm_mc_set(cdev, cdev->brightness);
0159 if (ret)
0160 return dev_err_probe(&pdev->dev, ret,
0161 "failed to set led PWM value for %s: %d",
0162 cdev->name, ret);
0163
0164 platform_set_drvdata(pdev, priv);
0165 return 0;
0166
0167 release_mcnode:
0168 fwnode_handle_put(mcnode);
0169 return ret;
0170 }
0171
0172 static const struct of_device_id of_pwm_leds_mc_match[] = {
0173 { .compatible = "pwm-leds-multicolor", },
0174 {}
0175 };
0176 MODULE_DEVICE_TABLE(of, of_pwm_leds_mc_match);
0177
0178 static struct platform_driver led_pwm_mc_driver = {
0179 .probe = led_pwm_mc_probe,
0180 .driver = {
0181 .name = "leds_pwm_multicolor",
0182 .of_match_table = of_pwm_leds_mc_match,
0183 },
0184 };
0185 module_platform_driver(led_pwm_mc_driver);
0186
0187 MODULE_AUTHOR("Sven Schwermer <sven.schwermer@disruptive-technologies.com>");
0188 MODULE_DESCRIPTION("multi-color PWM LED driver");
0189 MODULE_LICENSE("GPL v2");
0190 MODULE_ALIAS("platform:leds-pwm-multicolor");