0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/clk.h>
0013 #include <linux/delay.h>
0014 #include <linux/firmware.h>
0015 #include <linux/i2c.h>
0016 #include <linux/leds.h>
0017 #include <linux/module.h>
0018 #include <linux/platform_data/leds-lp55xx.h>
0019 #include <linux/slab.h>
0020 #include <linux/gpio/consumer.h>
0021
0022 #include "leds-lp55xx-common.h"
0023
0024
0025 #define LP55XX_CLK_32K 32768
0026
0027 static struct lp55xx_led *cdev_to_lp55xx_led(struct led_classdev *cdev)
0028 {
0029 return container_of(cdev, struct lp55xx_led, cdev);
0030 }
0031
0032 static struct lp55xx_led *dev_to_lp55xx_led(struct device *dev)
0033 {
0034 return cdev_to_lp55xx_led(dev_get_drvdata(dev));
0035 }
0036
0037 static struct lp55xx_led *mcled_cdev_to_led(struct led_classdev_mc *mc_cdev)
0038 {
0039 return container_of(mc_cdev, struct lp55xx_led, mc_cdev);
0040 }
0041
0042 static void lp55xx_reset_device(struct lp55xx_chip *chip)
0043 {
0044 struct lp55xx_device_config *cfg = chip->cfg;
0045 u8 addr = cfg->reset.addr;
0046 u8 val = cfg->reset.val;
0047
0048
0049 lp55xx_write(chip, addr, val);
0050 }
0051
0052 static int lp55xx_detect_device(struct lp55xx_chip *chip)
0053 {
0054 struct lp55xx_device_config *cfg = chip->cfg;
0055 u8 addr = cfg->enable.addr;
0056 u8 val = cfg->enable.val;
0057 int ret;
0058
0059 ret = lp55xx_write(chip, addr, val);
0060 if (ret)
0061 return ret;
0062
0063 usleep_range(1000, 2000);
0064
0065 ret = lp55xx_read(chip, addr, &val);
0066 if (ret)
0067 return ret;
0068
0069 if (val != cfg->enable.val)
0070 return -ENODEV;
0071
0072 return 0;
0073 }
0074
0075 static int lp55xx_post_init_device(struct lp55xx_chip *chip)
0076 {
0077 struct lp55xx_device_config *cfg = chip->cfg;
0078
0079 if (!cfg->post_init_device)
0080 return 0;
0081
0082 return cfg->post_init_device(chip);
0083 }
0084
0085 static ssize_t led_current_show(struct device *dev,
0086 struct device_attribute *attr,
0087 char *buf)
0088 {
0089 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
0090
0091 return scnprintf(buf, PAGE_SIZE, "%d\n", led->led_current);
0092 }
0093
0094 static ssize_t led_current_store(struct device *dev,
0095 struct device_attribute *attr,
0096 const char *buf, size_t len)
0097 {
0098 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
0099 struct lp55xx_chip *chip = led->chip;
0100 unsigned long curr;
0101
0102 if (kstrtoul(buf, 0, &curr))
0103 return -EINVAL;
0104
0105 if (curr > led->max_current)
0106 return -EINVAL;
0107
0108 if (!chip->cfg->set_led_current)
0109 return len;
0110
0111 mutex_lock(&chip->lock);
0112 chip->cfg->set_led_current(led, (u8)curr);
0113 mutex_unlock(&chip->lock);
0114
0115 return len;
0116 }
0117
0118 static ssize_t max_current_show(struct device *dev,
0119 struct device_attribute *attr,
0120 char *buf)
0121 {
0122 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
0123
0124 return scnprintf(buf, PAGE_SIZE, "%d\n", led->max_current);
0125 }
0126
0127 static DEVICE_ATTR_RW(led_current);
0128 static DEVICE_ATTR_RO(max_current);
0129
0130 static struct attribute *lp55xx_led_attrs[] = {
0131 &dev_attr_led_current.attr,
0132 &dev_attr_max_current.attr,
0133 NULL,
0134 };
0135 ATTRIBUTE_GROUPS(lp55xx_led);
0136
0137 static int lp55xx_set_mc_brightness(struct led_classdev *cdev,
0138 enum led_brightness brightness)
0139 {
0140 struct led_classdev_mc *mc_dev = lcdev_to_mccdev(cdev);
0141 struct lp55xx_led *led = mcled_cdev_to_led(mc_dev);
0142 struct lp55xx_device_config *cfg = led->chip->cfg;
0143
0144 led_mc_calc_color_components(&led->mc_cdev, brightness);
0145 return cfg->multicolor_brightness_fn(led);
0146
0147 }
0148
0149 static int lp55xx_set_brightness(struct led_classdev *cdev,
0150 enum led_brightness brightness)
0151 {
0152 struct lp55xx_led *led = cdev_to_lp55xx_led(cdev);
0153 struct lp55xx_device_config *cfg = led->chip->cfg;
0154
0155 led->brightness = (u8)brightness;
0156 return cfg->brightness_fn(led);
0157 }
0158
0159 static int lp55xx_init_led(struct lp55xx_led *led,
0160 struct lp55xx_chip *chip, int chan)
0161 {
0162 struct lp55xx_platform_data *pdata = chip->pdata;
0163 struct lp55xx_device_config *cfg = chip->cfg;
0164 struct device *dev = &chip->cl->dev;
0165 int max_channel = cfg->max_channel;
0166 struct mc_subled *mc_led_info;
0167 struct led_classdev *led_cdev;
0168 char name[32];
0169 int i, j = 0;
0170 int ret;
0171
0172 if (chan >= max_channel) {
0173 dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
0174 return -EINVAL;
0175 }
0176
0177 if (pdata->led_config[chan].led_current == 0)
0178 return 0;
0179
0180 if (pdata->led_config[chan].name) {
0181 led->cdev.name = pdata->led_config[chan].name;
0182 } else {
0183 snprintf(name, sizeof(name), "%s:channel%d",
0184 pdata->label ? : chip->cl->name, chan);
0185 led->cdev.name = name;
0186 }
0187
0188 if (pdata->led_config[chan].num_colors > 1) {
0189 mc_led_info = devm_kcalloc(dev,
0190 pdata->led_config[chan].num_colors,
0191 sizeof(*mc_led_info), GFP_KERNEL);
0192 if (!mc_led_info)
0193 return -ENOMEM;
0194
0195 led_cdev = &led->mc_cdev.led_cdev;
0196 led_cdev->name = led->cdev.name;
0197 led_cdev->brightness_set_blocking = lp55xx_set_mc_brightness;
0198 led->mc_cdev.num_colors = pdata->led_config[chan].num_colors;
0199 for (i = 0; i < led->mc_cdev.num_colors; i++) {
0200 mc_led_info[i].color_index =
0201 pdata->led_config[chan].color_id[i];
0202 mc_led_info[i].channel =
0203 pdata->led_config[chan].output_num[i];
0204 j++;
0205 }
0206
0207 led->mc_cdev.subled_info = mc_led_info;
0208 } else {
0209 led->cdev.brightness_set_blocking = lp55xx_set_brightness;
0210 }
0211
0212 led->cdev.groups = lp55xx_led_groups;
0213 led->cdev.default_trigger = pdata->led_config[chan].default_trigger;
0214 led->led_current = pdata->led_config[chan].led_current;
0215 led->max_current = pdata->led_config[chan].max_current;
0216 led->chan_nr = pdata->led_config[chan].chan_nr;
0217
0218 if (led->chan_nr >= max_channel) {
0219 dev_err(dev, "Use channel numbers between 0 and %d\n",
0220 max_channel - 1);
0221 return -EINVAL;
0222 }
0223
0224 if (pdata->led_config[chan].num_colors > 1)
0225 ret = devm_led_classdev_multicolor_register(dev, &led->mc_cdev);
0226 else
0227 ret = devm_led_classdev_register(dev, &led->cdev);
0228
0229 if (ret) {
0230 dev_err(dev, "led register err: %d\n", ret);
0231 return ret;
0232 }
0233
0234 return 0;
0235 }
0236
0237 static void lp55xx_firmware_loaded(const struct firmware *fw, void *context)
0238 {
0239 struct lp55xx_chip *chip = context;
0240 struct device *dev = &chip->cl->dev;
0241 enum lp55xx_engine_index idx = chip->engine_idx;
0242
0243 if (!fw) {
0244 dev_err(dev, "firmware request failed\n");
0245 return;
0246 }
0247
0248
0249 mutex_lock(&chip->lock);
0250
0251 chip->engines[idx - 1].mode = LP55XX_ENGINE_LOAD;
0252 chip->fw = fw;
0253 if (chip->cfg->firmware_cb)
0254 chip->cfg->firmware_cb(chip);
0255
0256 mutex_unlock(&chip->lock);
0257
0258
0259 release_firmware(chip->fw);
0260 chip->fw = NULL;
0261 }
0262
0263 static int lp55xx_request_firmware(struct lp55xx_chip *chip)
0264 {
0265 const char *name = chip->cl->name;
0266 struct device *dev = &chip->cl->dev;
0267
0268 return request_firmware_nowait(THIS_MODULE, false, name, dev,
0269 GFP_KERNEL, chip, lp55xx_firmware_loaded);
0270 }
0271
0272 static ssize_t select_engine_show(struct device *dev,
0273 struct device_attribute *attr,
0274 char *buf)
0275 {
0276 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
0277 struct lp55xx_chip *chip = led->chip;
0278
0279 return sprintf(buf, "%d\n", chip->engine_idx);
0280 }
0281
0282 static ssize_t select_engine_store(struct device *dev,
0283 struct device_attribute *attr,
0284 const char *buf, size_t len)
0285 {
0286 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
0287 struct lp55xx_chip *chip = led->chip;
0288 unsigned long val;
0289 int ret;
0290
0291 if (kstrtoul(buf, 0, &val))
0292 return -EINVAL;
0293
0294
0295
0296 switch (val) {
0297 case LP55XX_ENGINE_1:
0298 case LP55XX_ENGINE_2:
0299 case LP55XX_ENGINE_3:
0300 mutex_lock(&chip->lock);
0301 chip->engine_idx = val;
0302 ret = lp55xx_request_firmware(chip);
0303 mutex_unlock(&chip->lock);
0304 break;
0305 default:
0306 dev_err(dev, "%lu: invalid engine index. (1, 2, 3)\n", val);
0307 return -EINVAL;
0308 }
0309
0310 if (ret) {
0311 dev_err(dev, "request firmware err: %d\n", ret);
0312 return ret;
0313 }
0314
0315 return len;
0316 }
0317
0318 static inline void lp55xx_run_engine(struct lp55xx_chip *chip, bool start)
0319 {
0320 if (chip->cfg->run_engine)
0321 chip->cfg->run_engine(chip, start);
0322 }
0323
0324 static ssize_t run_engine_store(struct device *dev,
0325 struct device_attribute *attr,
0326 const char *buf, size_t len)
0327 {
0328 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
0329 struct lp55xx_chip *chip = led->chip;
0330 unsigned long val;
0331
0332 if (kstrtoul(buf, 0, &val))
0333 return -EINVAL;
0334
0335
0336
0337 if (val <= 0) {
0338 lp55xx_run_engine(chip, false);
0339 return len;
0340 }
0341
0342 mutex_lock(&chip->lock);
0343 lp55xx_run_engine(chip, true);
0344 mutex_unlock(&chip->lock);
0345
0346 return len;
0347 }
0348
0349 static DEVICE_ATTR_RW(select_engine);
0350 static DEVICE_ATTR_WO(run_engine);
0351
0352 static struct attribute *lp55xx_engine_attributes[] = {
0353 &dev_attr_select_engine.attr,
0354 &dev_attr_run_engine.attr,
0355 NULL,
0356 };
0357
0358 static const struct attribute_group lp55xx_engine_attr_group = {
0359 .attrs = lp55xx_engine_attributes,
0360 };
0361
0362 int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
0363 {
0364 return i2c_smbus_write_byte_data(chip->cl, reg, val);
0365 }
0366 EXPORT_SYMBOL_GPL(lp55xx_write);
0367
0368 int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
0369 {
0370 s32 ret;
0371
0372 ret = i2c_smbus_read_byte_data(chip->cl, reg);
0373 if (ret < 0)
0374 return ret;
0375
0376 *val = ret;
0377 return 0;
0378 }
0379 EXPORT_SYMBOL_GPL(lp55xx_read);
0380
0381 int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
0382 {
0383 int ret;
0384 u8 tmp;
0385
0386 ret = lp55xx_read(chip, reg, &tmp);
0387 if (ret)
0388 return ret;
0389
0390 tmp &= ~mask;
0391 tmp |= val & mask;
0392
0393 return lp55xx_write(chip, reg, tmp);
0394 }
0395 EXPORT_SYMBOL_GPL(lp55xx_update_bits);
0396
0397 bool lp55xx_is_extclk_used(struct lp55xx_chip *chip)
0398 {
0399 struct clk *clk;
0400 int err;
0401
0402 clk = devm_clk_get(&chip->cl->dev, "32k_clk");
0403 if (IS_ERR(clk))
0404 goto use_internal_clk;
0405
0406 err = clk_prepare_enable(clk);
0407 if (err)
0408 goto use_internal_clk;
0409
0410 if (clk_get_rate(clk) != LP55XX_CLK_32K) {
0411 clk_disable_unprepare(clk);
0412 goto use_internal_clk;
0413 }
0414
0415 dev_info(&chip->cl->dev, "%dHz external clock used\n", LP55XX_CLK_32K);
0416
0417 chip->clk = clk;
0418 return true;
0419
0420 use_internal_clk:
0421 dev_info(&chip->cl->dev, "internal clock used\n");
0422 return false;
0423 }
0424 EXPORT_SYMBOL_GPL(lp55xx_is_extclk_used);
0425
0426 int lp55xx_init_device(struct lp55xx_chip *chip)
0427 {
0428 struct lp55xx_platform_data *pdata;
0429 struct lp55xx_device_config *cfg;
0430 struct device *dev = &chip->cl->dev;
0431 int ret = 0;
0432
0433 WARN_ON(!chip);
0434
0435 pdata = chip->pdata;
0436 cfg = chip->cfg;
0437
0438 if (!pdata || !cfg)
0439 return -EINVAL;
0440
0441 if (pdata->enable_gpiod) {
0442 gpiod_direction_output(pdata->enable_gpiod, 0);
0443
0444 gpiod_set_consumer_name(pdata->enable_gpiod, "LP55xx enable");
0445 gpiod_set_value(pdata->enable_gpiod, 0);
0446 usleep_range(1000, 2000);
0447 gpiod_set_value(pdata->enable_gpiod, 1);
0448 usleep_range(1000, 2000);
0449 }
0450
0451 lp55xx_reset_device(chip);
0452
0453
0454
0455
0456
0457 usleep_range(10000, 20000);
0458
0459 ret = lp55xx_detect_device(chip);
0460 if (ret) {
0461 dev_err(dev, "device detection err: %d\n", ret);
0462 goto err;
0463 }
0464
0465
0466 ret = lp55xx_post_init_device(chip);
0467 if (ret) {
0468 dev_err(dev, "post init device err: %d\n", ret);
0469 goto err_post_init;
0470 }
0471
0472 return 0;
0473
0474 err_post_init:
0475 lp55xx_deinit_device(chip);
0476 err:
0477 return ret;
0478 }
0479 EXPORT_SYMBOL_GPL(lp55xx_init_device);
0480
0481 void lp55xx_deinit_device(struct lp55xx_chip *chip)
0482 {
0483 struct lp55xx_platform_data *pdata = chip->pdata;
0484
0485 if (chip->clk)
0486 clk_disable_unprepare(chip->clk);
0487
0488 if (pdata->enable_gpiod)
0489 gpiod_set_value(pdata->enable_gpiod, 0);
0490 }
0491 EXPORT_SYMBOL_GPL(lp55xx_deinit_device);
0492
0493 int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
0494 {
0495 struct lp55xx_platform_data *pdata = chip->pdata;
0496 struct lp55xx_device_config *cfg = chip->cfg;
0497 int num_channels = pdata->num_channels;
0498 struct lp55xx_led *each;
0499 u8 led_current;
0500 int ret;
0501 int i;
0502
0503 if (!cfg->brightness_fn) {
0504 dev_err(&chip->cl->dev, "empty brightness configuration\n");
0505 return -EINVAL;
0506 }
0507
0508 for (i = 0; i < num_channels; i++) {
0509
0510
0511 if (pdata->led_config[i].led_current == 0)
0512 continue;
0513
0514 led_current = pdata->led_config[i].led_current;
0515 each = led + i;
0516 ret = lp55xx_init_led(each, chip, i);
0517 if (ret)
0518 goto err_init_led;
0519
0520 chip->num_leds++;
0521 each->chip = chip;
0522
0523
0524 if (cfg->set_led_current)
0525 cfg->set_led_current(each, led_current);
0526 }
0527
0528 return 0;
0529
0530 err_init_led:
0531 return ret;
0532 }
0533 EXPORT_SYMBOL_GPL(lp55xx_register_leds);
0534
0535 int lp55xx_register_sysfs(struct lp55xx_chip *chip)
0536 {
0537 struct device *dev = &chip->cl->dev;
0538 struct lp55xx_device_config *cfg = chip->cfg;
0539 int ret;
0540
0541 if (!cfg->run_engine || !cfg->firmware_cb)
0542 goto dev_specific_attrs;
0543
0544 ret = sysfs_create_group(&dev->kobj, &lp55xx_engine_attr_group);
0545 if (ret)
0546 return ret;
0547
0548 dev_specific_attrs:
0549 return cfg->dev_attr_group ?
0550 sysfs_create_group(&dev->kobj, cfg->dev_attr_group) : 0;
0551 }
0552 EXPORT_SYMBOL_GPL(lp55xx_register_sysfs);
0553
0554 void lp55xx_unregister_sysfs(struct lp55xx_chip *chip)
0555 {
0556 struct device *dev = &chip->cl->dev;
0557 struct lp55xx_device_config *cfg = chip->cfg;
0558
0559 if (cfg->dev_attr_group)
0560 sysfs_remove_group(&dev->kobj, cfg->dev_attr_group);
0561
0562 sysfs_remove_group(&dev->kobj, &lp55xx_engine_attr_group);
0563 }
0564 EXPORT_SYMBOL_GPL(lp55xx_unregister_sysfs);
0565
0566 static int lp55xx_parse_common_child(struct device_node *np,
0567 struct lp55xx_led_config *cfg,
0568 int led_number, int *chan_nr)
0569 {
0570 int ret;
0571
0572 of_property_read_string(np, "chan-name",
0573 &cfg[led_number].name);
0574 of_property_read_u8(np, "led-cur",
0575 &cfg[led_number].led_current);
0576 of_property_read_u8(np, "max-cur",
0577 &cfg[led_number].max_current);
0578
0579 ret = of_property_read_u32(np, "reg", chan_nr);
0580 if (ret)
0581 return ret;
0582
0583 if (*chan_nr < 0 || *chan_nr > cfg->max_channel)
0584 return -EINVAL;
0585
0586 return 0;
0587 }
0588
0589 static int lp55xx_parse_multi_led_child(struct device_node *child,
0590 struct lp55xx_led_config *cfg,
0591 int child_number, int color_number)
0592 {
0593 int chan_nr, color_id, ret;
0594
0595 ret = lp55xx_parse_common_child(child, cfg, child_number, &chan_nr);
0596 if (ret)
0597 return ret;
0598
0599 ret = of_property_read_u32(child, "color", &color_id);
0600 if (ret)
0601 return ret;
0602
0603 cfg[child_number].color_id[color_number] = color_id;
0604 cfg[child_number].output_num[color_number] = chan_nr;
0605
0606 return 0;
0607 }
0608
0609 static int lp55xx_parse_multi_led(struct device_node *np,
0610 struct lp55xx_led_config *cfg,
0611 int child_number)
0612 {
0613 struct device_node *child;
0614 int num_colors = 0, ret;
0615
0616 for_each_available_child_of_node(np, child) {
0617 ret = lp55xx_parse_multi_led_child(child, cfg, child_number,
0618 num_colors);
0619 if (ret) {
0620 of_node_put(child);
0621 return ret;
0622 }
0623 num_colors++;
0624 }
0625
0626 cfg[child_number].num_colors = num_colors;
0627
0628 return 0;
0629 }
0630
0631 static int lp55xx_parse_logical_led(struct device_node *np,
0632 struct lp55xx_led_config *cfg,
0633 int child_number)
0634 {
0635 int led_color, ret;
0636 int chan_nr = 0;
0637
0638 cfg[child_number].default_trigger =
0639 of_get_property(np, "linux,default-trigger", NULL);
0640
0641 ret = of_property_read_u32(np, "color", &led_color);
0642 if (ret)
0643 return ret;
0644
0645 if (led_color == LED_COLOR_ID_RGB)
0646 return lp55xx_parse_multi_led(np, cfg, child_number);
0647
0648 ret = lp55xx_parse_common_child(np, cfg, child_number, &chan_nr);
0649 if (ret < 0)
0650 return ret;
0651
0652 cfg[child_number].chan_nr = chan_nr;
0653
0654 return ret;
0655 }
0656
0657 struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev,
0658 struct device_node *np,
0659 struct lp55xx_chip *chip)
0660 {
0661 struct device_node *child;
0662 struct lp55xx_platform_data *pdata;
0663 struct lp55xx_led_config *cfg;
0664 int num_channels;
0665 int i = 0;
0666 int ret;
0667
0668 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
0669 if (!pdata)
0670 return ERR_PTR(-ENOMEM);
0671
0672 num_channels = of_get_available_child_count(np);
0673 if (num_channels == 0) {
0674 dev_err(dev, "no LED channels\n");
0675 return ERR_PTR(-EINVAL);
0676 }
0677
0678 cfg = devm_kcalloc(dev, num_channels, sizeof(*cfg), GFP_KERNEL);
0679 if (!cfg)
0680 return ERR_PTR(-ENOMEM);
0681
0682 pdata->led_config = &cfg[0];
0683 pdata->num_channels = num_channels;
0684 cfg->max_channel = chip->cfg->max_channel;
0685
0686 for_each_available_child_of_node(np, child) {
0687 ret = lp55xx_parse_logical_led(child, cfg, i);
0688 if (ret) {
0689 of_node_put(child);
0690 return ERR_PTR(-EINVAL);
0691 }
0692 i++;
0693 }
0694
0695 of_property_read_string(np, "label", &pdata->label);
0696 of_property_read_u8(np, "clock-mode", &pdata->clock_mode);
0697
0698 pdata->enable_gpiod = devm_gpiod_get_optional(dev, "enable",
0699 GPIOD_ASIS);
0700 if (IS_ERR(pdata->enable_gpiod))
0701 return ERR_CAST(pdata->enable_gpiod);
0702
0703
0704 of_property_read_u8(np, "pwr-sel", (u8 *)&pdata->pwr_sel);
0705
0706 return pdata;
0707 }
0708 EXPORT_SYMBOL_GPL(lp55xx_of_populate_pdata);
0709
0710 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
0711 MODULE_DESCRIPTION("LP55xx Common Driver");
0712 MODULE_LICENSE("GPL");