leds-da903x.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * LEDs driver for Dialog Semiconductor DA9030/DA9034
  3. *
  4. * Copyright (C) 2008 Compulab, Ltd.
  5. * Mike Rapoport <mike@compulab.co.il>
  6. *
  7. * Copyright (C) 2006-2008 Marvell International Ltd.
  8. * Eric Miao <eric.miao@marvell.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/leds.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/mfd/da903x.h>
  21. #include <linux/slab.h>
  22. #define DA9030_LED1_CONTROL 0x20
  23. #define DA9030_LED2_CONTROL 0x21
  24. #define DA9030_LED3_CONTROL 0x22
  25. #define DA9030_LED4_CONTROL 0x23
  26. #define DA9030_LEDPC_CONTROL 0x24
  27. #define DA9030_MISC_CONTROL_A 0x26 /* Vibrator Control */
  28. #define DA9034_LED1_CONTROL 0x35
  29. #define DA9034_LED2_CONTROL 0x36
  30. #define DA9034_VIBRA 0x40
  31. struct da903x_led {
  32. struct led_classdev cdev;
  33. struct work_struct work;
  34. struct device *master;
  35. enum led_brightness new_brightness;
  36. int id;
  37. int flags;
  38. };
  39. #define DA9030_LED_OFFSET(id) ((id) - DA9030_ID_LED_1)
  40. #define DA9034_LED_OFFSET(id) ((id) - DA9034_ID_LED_1)
  41. static void da903x_led_work(struct work_struct *work)
  42. {
  43. struct da903x_led *led = container_of(work, struct da903x_led, work);
  44. uint8_t val;
  45. int offset;
  46. switch (led->id) {
  47. case DA9030_ID_LED_1:
  48. case DA9030_ID_LED_2:
  49. case DA9030_ID_LED_3:
  50. case DA9030_ID_LED_4:
  51. case DA9030_ID_LED_PC:
  52. offset = DA9030_LED_OFFSET(led->id);
  53. val = led->flags & ~0x87;
  54. val |= (led->new_brightness) ? 0x80 : 0; /* EN bit */
  55. val |= (0x7 - (led->new_brightness >> 5)) & 0x7; /* PWM<2:0> */
  56. da903x_write(led->master, DA9030_LED1_CONTROL + offset, val);
  57. break;
  58. case DA9030_ID_VIBRA:
  59. val = led->flags & ~0x80;
  60. val |= (led->new_brightness) ? 0x80 : 0; /* EN bit */
  61. da903x_write(led->master, DA9030_MISC_CONTROL_A, val);
  62. break;
  63. case DA9034_ID_LED_1:
  64. case DA9034_ID_LED_2:
  65. offset = DA9034_LED_OFFSET(led->id);
  66. val = (led->new_brightness * 0x5f / LED_FULL) & 0x7f;
  67. val |= (led->flags & DA9034_LED_RAMP) ? 0x80 : 0;
  68. da903x_write(led->master, DA9034_LED1_CONTROL + offset, val);
  69. break;
  70. case DA9034_ID_VIBRA:
  71. val = led->new_brightness & 0xfe;
  72. da903x_write(led->master, DA9034_VIBRA, val);
  73. break;
  74. }
  75. }
  76. static void da903x_led_set(struct led_classdev *led_cdev,
  77. enum led_brightness value)
  78. {
  79. struct da903x_led *led;
  80. led = container_of(led_cdev, struct da903x_led, cdev);
  81. led->new_brightness = value;
  82. schedule_work(&led->work);
  83. }
  84. static int __devinit da903x_led_probe(struct platform_device *pdev)
  85. {
  86. struct led_info *pdata = pdev->dev.platform_data;
  87. struct da903x_led *led;
  88. int id, ret;
  89. if (pdata == NULL)
  90. return 0;
  91. id = pdev->id;
  92. if (!((id >= DA9030_ID_LED_1 && id <= DA9030_ID_VIBRA) ||
  93. (id >= DA9034_ID_LED_1 && id <= DA9034_ID_VIBRA))) {
  94. dev_err(&pdev->dev, "invalid LED ID (%d) specified\n", id);
  95. return -EINVAL;
  96. }
  97. led = kzalloc(sizeof(struct da903x_led), GFP_KERNEL);
  98. if (led == NULL) {
  99. dev_err(&pdev->dev, "failed to alloc memory for LED%d\n", id);
  100. return -ENOMEM;
  101. }
  102. led->cdev.name = pdata->name;
  103. led->cdev.default_trigger = pdata->default_trigger;
  104. led->cdev.brightness_set = da903x_led_set;
  105. led->cdev.brightness = LED_OFF;
  106. led->id = id;
  107. led->flags = pdata->flags;
  108. led->master = pdev->dev.parent;
  109. led->new_brightness = LED_OFF;
  110. INIT_WORK(&led->work, da903x_led_work);
  111. ret = led_classdev_register(led->master, &led->cdev);
  112. if (ret) {
  113. dev_err(&pdev->dev, "failed to register LED %d\n", id);
  114. goto err;
  115. }
  116. platform_set_drvdata(pdev, led);
  117. return 0;
  118. err:
  119. kfree(led);
  120. return ret;
  121. }
  122. static int __devexit da903x_led_remove(struct platform_device *pdev)
  123. {
  124. struct da903x_led *led = platform_get_drvdata(pdev);
  125. led_classdev_unregister(&led->cdev);
  126. kfree(led);
  127. return 0;
  128. }
  129. static struct platform_driver da903x_led_driver = {
  130. .driver = {
  131. .name = "da903x-led",
  132. .owner = THIS_MODULE,
  133. },
  134. .probe = da903x_led_probe,
  135. .remove = __devexit_p(da903x_led_remove),
  136. };
  137. module_platform_driver(da903x_led_driver);
  138. MODULE_DESCRIPTION("LEDs driver for Dialog Semiconductor DA9030/DA9034");
  139. MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>"
  140. "Mike Rapoport <mike@compulab.co.il>");
  141. MODULE_LICENSE("GPL");
  142. MODULE_ALIAS("platform:da903x-led");