leds-pm8058.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* Copyright (c) 2010, 2011, 2016 The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/leds.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm.h>
  18. #include <linux/regmap.h>
  19. #define PM8058_LED_TYPE_COMMON 0x00
  20. #define PM8058_LED_TYPE_KEYPAD 0x01
  21. #define PM8058_LED_TYPE_FLASH 0x02
  22. #define PM8058_LED_TYPE_COMMON_MASK 0xf8
  23. #define PM8058_LED_TYPE_KEYPAD_MASK 0xf0
  24. #define PM8058_LED_TYPE_COMMON_SHIFT 3
  25. #define PM8058_LED_TYPE_KEYPAD_SHIFT 4
  26. struct pm8058_led {
  27. struct regmap *map;
  28. u32 reg;
  29. u32 ledtype;
  30. struct led_classdev cdev;
  31. };
  32. static void pm8058_led_set(struct led_classdev *cled,
  33. enum led_brightness value)
  34. {
  35. struct pm8058_led *led;
  36. int ret = 0;
  37. unsigned int mask = 0;
  38. unsigned int val = 0;
  39. led = container_of(cled, struct pm8058_led, cdev);
  40. switch (led->ledtype) {
  41. case PM8058_LED_TYPE_COMMON:
  42. mask = PM8058_LED_TYPE_COMMON_MASK;
  43. val = value << PM8058_LED_TYPE_COMMON_SHIFT;
  44. break;
  45. case PM8058_LED_TYPE_KEYPAD:
  46. case PM8058_LED_TYPE_FLASH:
  47. mask = PM8058_LED_TYPE_KEYPAD_MASK;
  48. val = value << PM8058_LED_TYPE_KEYPAD_SHIFT;
  49. break;
  50. default:
  51. break;
  52. }
  53. ret = regmap_update_bits(led->map, led->reg, mask, val);
  54. if (ret)
  55. pr_err("Failed to set LED brightness\n");
  56. }
  57. static enum led_brightness pm8058_led_get(struct led_classdev *cled)
  58. {
  59. struct pm8058_led *led;
  60. int ret;
  61. unsigned int val;
  62. led = container_of(cled, struct pm8058_led, cdev);
  63. ret = regmap_read(led->map, led->reg, &val);
  64. if (ret) {
  65. pr_err("Failed to get LED brightness\n");
  66. return LED_OFF;
  67. }
  68. switch (led->ledtype) {
  69. case PM8058_LED_TYPE_COMMON:
  70. val &= PM8058_LED_TYPE_COMMON_MASK;
  71. val >>= PM8058_LED_TYPE_COMMON_SHIFT;
  72. break;
  73. case PM8058_LED_TYPE_KEYPAD:
  74. case PM8058_LED_TYPE_FLASH:
  75. val &= PM8058_LED_TYPE_KEYPAD_MASK;
  76. val >>= PM8058_LED_TYPE_KEYPAD_SHIFT;
  77. break;
  78. default:
  79. val = LED_OFF;
  80. break;
  81. }
  82. return val;
  83. }
  84. static int pm8058_led_probe(struct platform_device *pdev)
  85. {
  86. struct pm8058_led *led;
  87. struct device_node *np = pdev->dev.of_node;
  88. int ret;
  89. struct regmap *map;
  90. const char *state;
  91. enum led_brightness maxbright;
  92. led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL);
  93. if (!led)
  94. return -ENOMEM;
  95. led->ledtype = (u32)(unsigned long)of_device_get_match_data(&pdev->dev);
  96. map = dev_get_regmap(pdev->dev.parent, NULL);
  97. if (!map) {
  98. dev_err(&pdev->dev, "Parent regmap unavailable.\n");
  99. return -ENXIO;
  100. }
  101. led->map = map;
  102. ret = of_property_read_u32(np, "reg", &led->reg);
  103. if (ret) {
  104. dev_err(&pdev->dev, "no register offset specified\n");
  105. return -EINVAL;
  106. }
  107. /* Use label else node name */
  108. led->cdev.name = of_get_property(np, "label", NULL) ? : np->name;
  109. led->cdev.default_trigger =
  110. of_get_property(np, "linux,default-trigger", NULL);
  111. led->cdev.brightness_set = pm8058_led_set;
  112. led->cdev.brightness_get = pm8058_led_get;
  113. if (led->ledtype == PM8058_LED_TYPE_COMMON)
  114. maxbright = 31; /* 5 bits */
  115. else
  116. maxbright = 15; /* 4 bits */
  117. led->cdev.max_brightness = maxbright;
  118. state = of_get_property(np, "default-state", NULL);
  119. if (state) {
  120. if (!strcmp(state, "keep")) {
  121. led->cdev.brightness = pm8058_led_get(&led->cdev);
  122. } else if (!strcmp(state, "on")) {
  123. led->cdev.brightness = maxbright;
  124. pm8058_led_set(&led->cdev, maxbright);
  125. } else {
  126. led->cdev.brightness = LED_OFF;
  127. pm8058_led_set(&led->cdev, LED_OFF);
  128. }
  129. }
  130. if (led->ledtype == PM8058_LED_TYPE_KEYPAD ||
  131. led->ledtype == PM8058_LED_TYPE_FLASH)
  132. led->cdev.flags = LED_CORE_SUSPENDRESUME;
  133. ret = devm_led_classdev_register(&pdev->dev, &led->cdev);
  134. if (ret) {
  135. dev_err(&pdev->dev, "unable to register led \"%s\"\n",
  136. led->cdev.name);
  137. return ret;
  138. }
  139. return 0;
  140. }
  141. static const struct of_device_id pm8058_leds_id_table[] = {
  142. {
  143. .compatible = "qcom,pm8058-led",
  144. .data = (void *)PM8058_LED_TYPE_COMMON
  145. },
  146. {
  147. .compatible = "qcom,pm8058-keypad-led",
  148. .data = (void *)PM8058_LED_TYPE_KEYPAD
  149. },
  150. {
  151. .compatible = "qcom,pm8058-flash-led",
  152. .data = (void *)PM8058_LED_TYPE_FLASH
  153. },
  154. { },
  155. };
  156. MODULE_DEVICE_TABLE(of, pm8058_leds_id_table);
  157. static struct platform_driver pm8058_led_driver = {
  158. .probe = pm8058_led_probe,
  159. .driver = {
  160. .name = "pm8058-leds",
  161. .of_match_table = pm8058_leds_id_table,
  162. },
  163. };
  164. module_platform_driver(pm8058_led_driver);
  165. MODULE_DESCRIPTION("PM8058 LEDs driver");
  166. MODULE_LICENSE("GPL v2");
  167. MODULE_ALIAS("platform:pm8058-leds");