leds-lm36274.c 4.2 KB

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