leds-da903x.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/platform_device.h>
  17. #include <linux/leds.h>
  18. #include <linux/mfd/da903x.h>
  19. #include <linux/slab.h>
  20. #define DA9030_LED1_CONTROL 0x20
  21. #define DA9030_LED2_CONTROL 0x21
  22. #define DA9030_LED3_CONTROL 0x22
  23. #define DA9030_LED4_CONTROL 0x23
  24. #define DA9030_LEDPC_CONTROL 0x24
  25. #define DA9030_MISC_CONTROL_A 0x26 /* Vibrator Control */
  26. #define DA9034_LED1_CONTROL 0x35
  27. #define DA9034_LED2_CONTROL 0x36
  28. #define DA9034_VIBRA 0x40
  29. struct da903x_led {
  30. struct led_classdev cdev;
  31. struct device *master;
  32. int id;
  33. int flags;
  34. };
  35. #define DA9030_LED_OFFSET(id) ((id) - DA9030_ID_LED_1)
  36. #define DA9034_LED_OFFSET(id) ((id) - DA9034_ID_LED_1)
  37. static int da903x_led_set(struct led_classdev *led_cdev,
  38. enum led_brightness value)
  39. {
  40. struct da903x_led *led =
  41. container_of(led_cdev, struct da903x_led, cdev);
  42. uint8_t val;
  43. int offset, ret = -EINVAL;
  44. switch (led->id) {
  45. case DA9030_ID_LED_1:
  46. case DA9030_ID_LED_2:
  47. case DA9030_ID_LED_3:
  48. case DA9030_ID_LED_4:
  49. case DA9030_ID_LED_PC:
  50. offset = DA9030_LED_OFFSET(led->id);
  51. val = led->flags & ~0x87;
  52. val |= value ? 0x80 : 0; /* EN bit */
  53. val |= (0x7 - (value >> 5)) & 0x7; /* PWM<2:0> */
  54. ret = da903x_write(led->master, DA9030_LED1_CONTROL + offset,
  55. val);
  56. break;
  57. case DA9030_ID_VIBRA:
  58. val = led->flags & ~0x80;
  59. val |= value ? 0x80 : 0; /* EN bit */
  60. ret = da903x_write(led->master, DA9030_MISC_CONTROL_A, val);
  61. break;
  62. case DA9034_ID_LED_1:
  63. case DA9034_ID_LED_2:
  64. offset = DA9034_LED_OFFSET(led->id);
  65. val = (value * 0x5f / LED_FULL) & 0x7f;
  66. val |= (led->flags & DA9034_LED_RAMP) ? 0x80 : 0;
  67. ret = da903x_write(led->master, DA9034_LED1_CONTROL + offset,
  68. val);
  69. break;
  70. case DA9034_ID_VIBRA:
  71. val = value & 0xfe;
  72. ret = da903x_write(led->master, DA9034_VIBRA, val);
  73. break;
  74. }
  75. return ret;
  76. }
  77. static int da903x_led_probe(struct platform_device *pdev)
  78. {
  79. struct led_info *pdata = dev_get_platdata(&pdev->dev);
  80. struct da903x_led *led;
  81. int id, ret;
  82. if (pdata == NULL)
  83. return 0;
  84. id = pdev->id;
  85. if (!((id >= DA9030_ID_LED_1 && id <= DA9030_ID_VIBRA) ||
  86. (id >= DA9034_ID_LED_1 && id <= DA9034_ID_VIBRA))) {
  87. dev_err(&pdev->dev, "invalid LED ID (%d) specified\n", id);
  88. return -EINVAL;
  89. }
  90. led = devm_kzalloc(&pdev->dev, sizeof(struct da903x_led), GFP_KERNEL);
  91. if (!led)
  92. return -ENOMEM;
  93. led->cdev.name = pdata->name;
  94. led->cdev.default_trigger = pdata->default_trigger;
  95. led->cdev.brightness_set_blocking = da903x_led_set;
  96. led->cdev.brightness = LED_OFF;
  97. led->id = id;
  98. led->flags = pdata->flags;
  99. led->master = pdev->dev.parent;
  100. ret = devm_led_classdev_register(led->master, &led->cdev);
  101. if (ret) {
  102. dev_err(&pdev->dev, "failed to register LED %d\n", id);
  103. return ret;
  104. }
  105. return 0;
  106. }
  107. static struct platform_driver da903x_led_driver = {
  108. .driver = {
  109. .name = "da903x-led",
  110. },
  111. .probe = da903x_led_probe,
  112. };
  113. module_platform_driver(da903x_led_driver);
  114. MODULE_DESCRIPTION("LEDs driver for Dialog Semiconductor DA9030/DA9034");
  115. MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
  116. MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
  117. MODULE_LICENSE("GPL");
  118. MODULE_ALIAS("platform:da903x-led");