leds-lm36274.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // SPDX-License-Identifier: GPL-2.0
  2. // TI LM36274 LED chip family driver
  3. // Copyright (C) 2019 Texas Instruments Incorporated - https://www.ti.com/
  4. #include <linux/bitops.h>
  5. #include <linux/device.h>
  6. #include <linux/err.h>
  7. #include <linux/leds.h>
  8. #include <linux/leds-ti-lmu-common.h>
  9. #include <linux/module.h>
  10. #include <linux/of_device.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/mfd/ti-lmu.h>
  13. #include <linux/mfd/ti-lmu-register.h>
  14. #include <uapi/linux/uleds.h>
  15. #define LM36274_MAX_STRINGS 4
  16. #define LM36274_BL_EN BIT(4)
  17. /**
  18. * struct lm36274
  19. * @pdev: platform device
  20. * @led_dev: led class device
  21. * @lmu_data: Register and setting values for common code
  22. * @regmap: Devices register map
  23. * @dev: Pointer to the devices device struct
  24. * @led_sources: The LED strings supported in this array
  25. * @num_leds: Number of LED strings are supported in this array
  26. */
  27. struct lm36274 {
  28. struct platform_device *pdev;
  29. struct led_classdev led_dev;
  30. struct ti_lmu_bank lmu_data;
  31. struct regmap *regmap;
  32. struct device *dev;
  33. u32 led_sources[LM36274_MAX_STRINGS];
  34. int num_leds;
  35. };
  36. static int lm36274_brightness_set(struct led_classdev *led_cdev,
  37. enum led_brightness brt_val)
  38. {
  39. struct lm36274 *chip = container_of(led_cdev, struct lm36274, led_dev);
  40. return ti_lmu_common_set_brightness(&chip->lmu_data, brt_val);
  41. }
  42. static int lm36274_init(struct lm36274 *chip)
  43. {
  44. int enable_val = 0;
  45. int i;
  46. for (i = 0; i < chip->num_leds; i++)
  47. enable_val |= (1 << chip->led_sources[i]);
  48. if (!enable_val) {
  49. dev_err(chip->dev, "No LEDs were enabled\n");
  50. return -EINVAL;
  51. }
  52. enable_val |= LM36274_BL_EN;
  53. return regmap_write(chip->regmap, LM36274_REG_BL_EN, enable_val);
  54. }
  55. static int lm36274_parse_dt(struct lm36274 *chip,
  56. struct led_init_data *init_data)
  57. {
  58. struct device *dev = chip->dev;
  59. struct fwnode_handle *child;
  60. int ret;
  61. /* There should only be 1 node */
  62. if (device_get_child_node_count(dev) != 1)
  63. return -EINVAL;
  64. child = device_get_next_child_node(dev, NULL);
  65. init_data->fwnode = child;
  66. init_data->devicename = chip->pdev->name;
  67. /* for backwards compatibility when `label` property is not present */
  68. init_data->default_label = ":";
  69. chip->num_leds = fwnode_property_count_u32(child, "led-sources");
  70. if (chip->num_leds <= 0) {
  71. ret = -ENODEV;
  72. goto err;
  73. }
  74. ret = fwnode_property_read_u32_array(child, "led-sources",
  75. chip->led_sources, chip->num_leds);
  76. if (ret) {
  77. dev_err(dev, "led-sources property missing\n");
  78. goto err;
  79. }
  80. return 0;
  81. err:
  82. fwnode_handle_put(child);
  83. return ret;
  84. }
  85. static int lm36274_probe(struct platform_device *pdev)
  86. {
  87. struct ti_lmu *lmu = dev_get_drvdata(pdev->dev.parent);
  88. struct led_init_data init_data = {};
  89. struct lm36274 *chip;
  90. int ret;
  91. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  92. if (!chip)
  93. return -ENOMEM;
  94. chip->pdev = pdev;
  95. chip->dev = &pdev->dev;
  96. chip->regmap = lmu->regmap;
  97. platform_set_drvdata(pdev, chip);
  98. ret = lm36274_parse_dt(chip, &init_data);
  99. if (ret) {
  100. dev_err(chip->dev, "Failed to parse DT node\n");
  101. return ret;
  102. }
  103. ret = lm36274_init(chip);
  104. if (ret) {
  105. fwnode_handle_put(init_data.fwnode);
  106. dev_err(chip->dev, "Failed to init the device\n");
  107. return ret;
  108. }
  109. chip->lmu_data.regmap = chip->regmap;
  110. chip->lmu_data.max_brightness = MAX_BRIGHTNESS_11BIT;
  111. chip->lmu_data.msb_brightness_reg = LM36274_REG_BRT_MSB;
  112. chip->lmu_data.lsb_brightness_reg = LM36274_REG_BRT_LSB;
  113. chip->led_dev.max_brightness = MAX_BRIGHTNESS_11BIT;
  114. chip->led_dev.brightness_set_blocking = lm36274_brightness_set;
  115. ret = devm_led_classdev_register_ext(chip->dev, &chip->led_dev,
  116. &init_data);
  117. if (ret)
  118. dev_err(chip->dev, "Failed to register LED for node %pfw\n",
  119. init_data.fwnode);
  120. fwnode_handle_put(init_data.fwnode);
  121. return ret;
  122. }
  123. static const struct of_device_id of_lm36274_leds_match[] = {
  124. { .compatible = "ti,lm36274-backlight", },
  125. {},
  126. };
  127. MODULE_DEVICE_TABLE(of, of_lm36274_leds_match);
  128. static struct platform_driver lm36274_driver = {
  129. .probe = lm36274_probe,
  130. .driver = {
  131. .name = "lm36274-leds",
  132. .of_match_table = of_lm36274_leds_match,
  133. },
  134. };
  135. module_platform_driver(lm36274_driver)
  136. MODULE_DESCRIPTION("Texas Instruments LM36274 LED driver");
  137. MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
  138. MODULE_LICENSE("GPL v2");