leds-da9052.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * LED Driver for Dialog DA9052 PMICs.
  3. *
  4. * Copyright(c) 2012 Dialog Semiconductor Ltd.
  5. *
  6. * Author: David Dajun Chen <[email protected]>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/leds.h>
  18. #include <linux/slab.h>
  19. #include <linux/mfd/da9052/reg.h>
  20. #include <linux/mfd/da9052/da9052.h>
  21. #include <linux/mfd/da9052/pdata.h>
  22. #define DA9052_OPENDRAIN_OUTPUT 2
  23. #define DA9052_SET_HIGH_LVL_OUTPUT (1 << 3)
  24. #define DA9052_MASK_UPPER_NIBBLE 0xF0
  25. #define DA9052_MASK_LOWER_NIBBLE 0x0F
  26. #define DA9052_NIBBLE_SHIFT 4
  27. #define DA9052_MAX_BRIGHTNESS 0x5f
  28. struct da9052_led {
  29. struct led_classdev cdev;
  30. struct da9052 *da9052;
  31. unsigned char led_index;
  32. unsigned char id;
  33. };
  34. static unsigned char led_reg[] = {
  35. DA9052_LED_CONT_4_REG,
  36. DA9052_LED_CONT_5_REG,
  37. };
  38. static int da9052_set_led_brightness(struct da9052_led *led,
  39. enum led_brightness brightness)
  40. {
  41. u8 val;
  42. int error;
  43. val = (brightness & 0x7f) | DA9052_LED_CONT_DIM;
  44. error = da9052_reg_write(led->da9052, led_reg[led->led_index], val);
  45. if (error < 0)
  46. dev_err(led->da9052->dev, "Failed to set led brightness, %d\n",
  47. error);
  48. return error;
  49. }
  50. static int da9052_led_set(struct led_classdev *led_cdev,
  51. enum led_brightness value)
  52. {
  53. struct da9052_led *led =
  54. container_of(led_cdev, struct da9052_led, cdev);
  55. return da9052_set_led_brightness(led, value);
  56. }
  57. static int da9052_configure_leds(struct da9052 *da9052)
  58. {
  59. int error;
  60. unsigned char register_value = DA9052_OPENDRAIN_OUTPUT
  61. | DA9052_SET_HIGH_LVL_OUTPUT;
  62. error = da9052_reg_update(da9052, DA9052_GPIO_14_15_REG,
  63. DA9052_MASK_LOWER_NIBBLE,
  64. register_value);
  65. if (error < 0) {
  66. dev_err(da9052->dev, "Failed to write GPIO 14-15 reg, %d\n",
  67. error);
  68. return error;
  69. }
  70. error = da9052_reg_update(da9052, DA9052_GPIO_14_15_REG,
  71. DA9052_MASK_UPPER_NIBBLE,
  72. register_value << DA9052_NIBBLE_SHIFT);
  73. if (error < 0)
  74. dev_err(da9052->dev, "Failed to write GPIO 14-15 reg, %d\n",
  75. error);
  76. return error;
  77. }
  78. static int da9052_led_probe(struct platform_device *pdev)
  79. {
  80. struct da9052_pdata *pdata;
  81. struct da9052 *da9052;
  82. struct led_platform_data *pled;
  83. struct da9052_led *led = NULL;
  84. int error = -ENODEV;
  85. int i;
  86. da9052 = dev_get_drvdata(pdev->dev.parent);
  87. pdata = dev_get_platdata(da9052->dev);
  88. if (pdata == NULL) {
  89. dev_err(&pdev->dev, "No platform data\n");
  90. goto err;
  91. }
  92. pled = pdata->pled;
  93. if (pled == NULL) {
  94. dev_err(&pdev->dev, "No platform data for LED\n");
  95. goto err;
  96. }
  97. led = devm_kzalloc(&pdev->dev,
  98. sizeof(struct da9052_led) * pled->num_leds,
  99. GFP_KERNEL);
  100. if (!led) {
  101. error = -ENOMEM;
  102. goto err;
  103. }
  104. for (i = 0; i < pled->num_leds; i++) {
  105. led[i].cdev.name = pled->leds[i].name;
  106. led[i].cdev.brightness_set_blocking = da9052_led_set;
  107. led[i].cdev.brightness = LED_OFF;
  108. led[i].cdev.max_brightness = DA9052_MAX_BRIGHTNESS;
  109. led[i].led_index = pled->leds[i].flags;
  110. led[i].da9052 = dev_get_drvdata(pdev->dev.parent);
  111. error = led_classdev_register(pdev->dev.parent, &led[i].cdev);
  112. if (error) {
  113. dev_err(&pdev->dev, "Failed to register led %d\n",
  114. led[i].led_index);
  115. goto err_register;
  116. }
  117. error = da9052_set_led_brightness(&led[i],
  118. led[i].cdev.brightness);
  119. if (error) {
  120. dev_err(&pdev->dev, "Unable to init led %d\n",
  121. led[i].led_index);
  122. continue;
  123. }
  124. }
  125. error = da9052_configure_leds(led->da9052);
  126. if (error) {
  127. dev_err(&pdev->dev, "Failed to configure GPIO LED%d\n", error);
  128. goto err_register;
  129. }
  130. platform_set_drvdata(pdev, led);
  131. return 0;
  132. err_register:
  133. for (i = i - 1; i >= 0; i--)
  134. led_classdev_unregister(&led[i].cdev);
  135. err:
  136. return error;
  137. }
  138. static int da9052_led_remove(struct platform_device *pdev)
  139. {
  140. struct da9052_led *led = platform_get_drvdata(pdev);
  141. struct da9052_pdata *pdata;
  142. struct da9052 *da9052;
  143. struct led_platform_data *pled;
  144. int i;
  145. da9052 = dev_get_drvdata(pdev->dev.parent);
  146. pdata = dev_get_platdata(da9052->dev);
  147. pled = pdata->pled;
  148. for (i = 0; i < pled->num_leds; i++) {
  149. da9052_set_led_brightness(&led[i], LED_OFF);
  150. led_classdev_unregister(&led[i].cdev);
  151. }
  152. return 0;
  153. }
  154. static struct platform_driver da9052_led_driver = {
  155. .driver = {
  156. .name = "da9052-leds",
  157. },
  158. .probe = da9052_led_probe,
  159. .remove = da9052_led_remove,
  160. };
  161. module_platform_driver(da9052_led_driver);
  162. MODULE_AUTHOR("Dialog Semiconductor Ltd <[email protected]>");
  163. MODULE_DESCRIPTION("LED driver for Dialog DA9052 PMIC");
  164. MODULE_LICENSE("GPL");