leds-da9052.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * LED Driver for Dialog DA9052 PMICs.
  3. *
  4. * Copyright(c) 2012 Dialog Semiconductor Ltd.
  5. *
  6. * Author: David Dajun Chen <dchen@diasemi.com>
  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/workqueue.h>
  19. #include <linux/slab.h>
  20. #include <linux/mfd/da9052/reg.h>
  21. #include <linux/mfd/da9052/da9052.h>
  22. #include <linux/mfd/da9052/pdata.h>
  23. #define DA9052_OPENDRAIN_OUTPUT 2
  24. #define DA9052_SET_HIGH_LVL_OUTPUT (1 << 3)
  25. #define DA9052_MASK_UPPER_NIBBLE 0xF0
  26. #define DA9052_MASK_LOWER_NIBBLE 0x0F
  27. #define DA9052_NIBBLE_SHIFT 4
  28. #define DA9052_MAX_BRIGHTNESS 0x5f
  29. struct da9052_led {
  30. struct led_classdev cdev;
  31. struct work_struct work;
  32. struct da9052 *da9052;
  33. unsigned char led_index;
  34. unsigned char id;
  35. int brightness;
  36. };
  37. static unsigned char led_reg[] = {
  38. DA9052_LED_CONT_4_REG,
  39. DA9052_LED_CONT_5_REG,
  40. };
  41. static int da9052_set_led_brightness(struct da9052_led *led)
  42. {
  43. u8 val;
  44. int error;
  45. val = (led->brightness & 0x7f) | DA9052_LED_CONT_DIM;
  46. error = da9052_reg_write(led->da9052, led_reg[led->led_index], val);
  47. if (error < 0)
  48. dev_err(led->da9052->dev, "Failed to set led brightness, %d\n",
  49. error);
  50. return error;
  51. }
  52. static void da9052_led_work(struct work_struct *work)
  53. {
  54. struct da9052_led *led = container_of(work, struct da9052_led, work);
  55. da9052_set_led_brightness(led);
  56. }
  57. static void da9052_led_set(struct led_classdev *led_cdev,
  58. enum led_brightness value)
  59. {
  60. struct da9052_led *led;
  61. led = container_of(led_cdev, struct da9052_led, cdev);
  62. led->brightness = value;
  63. schedule_work(&led->work);
  64. }
  65. static int da9052_configure_leds(struct da9052 *da9052)
  66. {
  67. int error;
  68. unsigned char register_value = DA9052_OPENDRAIN_OUTPUT
  69. | DA9052_SET_HIGH_LVL_OUTPUT;
  70. error = da9052_reg_update(da9052, DA9052_GPIO_14_15_REG,
  71. DA9052_MASK_LOWER_NIBBLE,
  72. register_value);
  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. error = da9052_reg_update(da9052, DA9052_GPIO_14_15_REG,
  79. DA9052_MASK_UPPER_NIBBLE,
  80. register_value << DA9052_NIBBLE_SHIFT);
  81. if (error < 0)
  82. dev_err(da9052->dev, "Failed to write GPIO 14-15 reg, %d\n",
  83. error);
  84. return error;
  85. }
  86. static int da9052_led_probe(struct platform_device *pdev)
  87. {
  88. struct da9052_pdata *pdata;
  89. struct da9052 *da9052;
  90. struct led_platform_data *pled;
  91. struct da9052_led *led = NULL;
  92. int error = -ENODEV;
  93. int i;
  94. da9052 = dev_get_drvdata(pdev->dev.parent);
  95. pdata = dev_get_platdata(da9052->dev);
  96. if (pdata == NULL) {
  97. dev_err(&pdev->dev, "No platform data\n");
  98. goto err;
  99. }
  100. pled = pdata->pled;
  101. if (pled == NULL) {
  102. dev_err(&pdev->dev, "No platform data for LED\n");
  103. goto err;
  104. }
  105. led = devm_kzalloc(&pdev->dev,
  106. sizeof(struct da9052_led) * pled->num_leds,
  107. GFP_KERNEL);
  108. if (!led) {
  109. error = -ENOMEM;
  110. goto err;
  111. }
  112. for (i = 0; i < pled->num_leds; i++) {
  113. led[i].cdev.name = pled->leds[i].name;
  114. led[i].cdev.brightness_set = da9052_led_set;
  115. led[i].cdev.brightness = LED_OFF;
  116. led[i].cdev.max_brightness = DA9052_MAX_BRIGHTNESS;
  117. led[i].brightness = LED_OFF;
  118. led[i].led_index = pled->leds[i].flags;
  119. led[i].da9052 = dev_get_drvdata(pdev->dev.parent);
  120. INIT_WORK(&led[i].work, da9052_led_work);
  121. error = led_classdev_register(pdev->dev.parent, &led[i].cdev);
  122. if (error) {
  123. dev_err(&pdev->dev, "Failed to register led %d\n",
  124. led[i].led_index);
  125. goto err_register;
  126. }
  127. error = da9052_set_led_brightness(&led[i]);
  128. if (error) {
  129. dev_err(&pdev->dev, "Unable to init led %d\n",
  130. led[i].led_index);
  131. continue;
  132. }
  133. }
  134. error = da9052_configure_leds(led->da9052);
  135. if (error) {
  136. dev_err(&pdev->dev, "Failed to configure GPIO LED%d\n", error);
  137. goto err_register;
  138. }
  139. platform_set_drvdata(pdev, led);
  140. return 0;
  141. err_register:
  142. for (i = i - 1; i >= 0; i--) {
  143. led_classdev_unregister(&led[i].cdev);
  144. cancel_work_sync(&led[i].work);
  145. }
  146. err:
  147. return error;
  148. }
  149. static int da9052_led_remove(struct platform_device *pdev)
  150. {
  151. struct da9052_led *led = platform_get_drvdata(pdev);
  152. struct da9052_pdata *pdata;
  153. struct da9052 *da9052;
  154. struct led_platform_data *pled;
  155. int i;
  156. da9052 = dev_get_drvdata(pdev->dev.parent);
  157. pdata = dev_get_platdata(da9052->dev);
  158. pled = pdata->pled;
  159. for (i = 0; i < pled->num_leds; i++) {
  160. led[i].brightness = 0;
  161. da9052_set_led_brightness(&led[i]);
  162. led_classdev_unregister(&led[i].cdev);
  163. cancel_work_sync(&led[i].work);
  164. }
  165. return 0;
  166. }
  167. static struct platform_driver da9052_led_driver = {
  168. .driver = {
  169. .name = "da9052-leds",
  170. },
  171. .probe = da9052_led_probe,
  172. .remove = da9052_led_remove,
  173. };
  174. module_platform_driver(da9052_led_driver);
  175. MODULE_AUTHOR("Dialog Semiconductor Ltd <dchen@diasemi.com>");
  176. MODULE_DESCRIPTION("LED driver for Dialog DA9052 PMIC");
  177. MODULE_LICENSE("GPL");