leds-pmic-mpp.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Copyright (c) 2012, 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/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/leds.h>
  16. #include <linux/slab.h>
  17. #include <linux/module.h>
  18. #include <mach/pmic.h>
  19. #define LED_MPP(x) ((x) & 0xFF)
  20. #define LED_CURR(x) ((x) >> 16)
  21. struct pmic_mpp_led_data {
  22. struct led_classdev cdev;
  23. int curr;
  24. int mpp;
  25. };
  26. static void pm_mpp_led_set(struct led_classdev *led_cdev,
  27. enum led_brightness value)
  28. {
  29. struct pmic_mpp_led_data *led;
  30. int ret;
  31. led = container_of(led_cdev, struct pmic_mpp_led_data, cdev);
  32. if (value < LED_OFF || value > led->cdev.max_brightness) {
  33. dev_err(led->cdev.dev, "Invalid brightness value");
  34. return;
  35. }
  36. ret = pmic_secure_mpp_config_i_sink(led->mpp, led->curr,
  37. value ? PM_MPP__I_SINK__SWITCH_ENA :
  38. PM_MPP__I_SINK__SWITCH_DIS);
  39. if (ret)
  40. dev_err(led_cdev->dev, "can't set mpp led\n");
  41. }
  42. static int pmic_mpp_led_probe(struct platform_device *pdev)
  43. {
  44. const struct led_platform_data *pdata = pdev->dev.platform_data;
  45. struct pmic_mpp_led_data *led, *tmp_led;
  46. int i, rc;
  47. if (!pdata) {
  48. dev_err(&pdev->dev, "platform data not supplied\n");
  49. return -EINVAL;
  50. }
  51. led = kcalloc(pdata->num_leds, sizeof(*led), GFP_KERNEL);
  52. if (!led) {
  53. dev_err(&pdev->dev, "failed to alloc memory\n");
  54. return -ENOMEM;
  55. }
  56. platform_set_drvdata(pdev, led);
  57. for (i = 0; i < pdata->num_leds; i++) {
  58. tmp_led = &led[i];
  59. tmp_led->cdev.name = pdata->leds[i].name;
  60. tmp_led->cdev.brightness_set = pm_mpp_led_set;
  61. tmp_led->cdev.brightness = LED_OFF;
  62. tmp_led->cdev.max_brightness = LED_FULL;
  63. tmp_led->mpp = LED_MPP(pdata->leds[i].flags);
  64. tmp_led->curr = LED_CURR(pdata->leds[i].flags);
  65. if (tmp_led->curr < PM_MPP__I_SINK__LEVEL_5mA ||
  66. tmp_led->curr > PM_MPP__I_SINK__LEVEL_40mA) {
  67. dev_err(&pdev->dev, "invalid current\n");
  68. goto unreg_led_cdev;
  69. }
  70. rc = led_classdev_register(&pdev->dev, &tmp_led->cdev);
  71. if (rc) {
  72. dev_err(&pdev->dev, "failed to register led\n");
  73. goto unreg_led_cdev;
  74. }
  75. }
  76. return 0;
  77. unreg_led_cdev:
  78. while (i)
  79. led_classdev_unregister(&led[--i].cdev);
  80. kfree(led);
  81. return rc;
  82. }
  83. static int __devexit pmic_mpp_led_remove(struct platform_device *pdev)
  84. {
  85. const struct led_platform_data *pdata = pdev->dev.platform_data;
  86. struct pmic_mpp_led_data *led = platform_get_drvdata(pdev);
  87. int i;
  88. for (i = 0; i < pdata->num_leds; i++)
  89. led_classdev_unregister(&led[i].cdev);
  90. kfree(led);
  91. return 0;
  92. }
  93. static struct platform_driver pmic_mpp_led_driver = {
  94. .probe = pmic_mpp_led_probe,
  95. .remove = __devexit_p(pmic_mpp_led_remove),
  96. .driver = {
  97. .name = "pmic-mpp-leds",
  98. .owner = THIS_MODULE,
  99. },
  100. };
  101. static int __init pmic_mpp_led_init(void)
  102. {
  103. return platform_driver_register(&pmic_mpp_led_driver);
  104. }
  105. module_init(pmic_mpp_led_init);
  106. static void __exit pmic_mpp_led_exit(void)
  107. {
  108. platform_driver_unregister(&pmic_mpp_led_driver);
  109. }
  110. module_exit(pmic_mpp_led_exit);
  111. MODULE_DESCRIPTION("PMIC MPP LEDs driver");
  112. MODULE_LICENSE("GPL v2");
  113. MODULE_ALIAS("platform:pmic-mpp-leds");