dev-backlight.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* linux/arch/arm/plat-samsung/dev-backlight.c
  2. *
  3. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com
  5. *
  6. * Common infrastructure for PWM Backlight for Samsung boards
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/gpio.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/io.h>
  16. #include <linux/pwm_backlight.h>
  17. #include <linux/slab.h>
  18. #include <plat/devs.h>
  19. #include <plat/gpio-cfg.h>
  20. #include <plat/backlight.h>
  21. static int samsung_bl_init(struct device *dev)
  22. {
  23. int ret = 0;
  24. struct platform_device *timer_dev =
  25. container_of(dev->parent, struct platform_device, dev);
  26. struct samsung_bl_gpio_info *bl_gpio_info =
  27. timer_dev->dev.platform_data;
  28. ret = gpio_request(bl_gpio_info->no, "Backlight");
  29. if (ret) {
  30. printk(KERN_ERR "failed to request GPIO for LCD Backlight\n");
  31. return ret;
  32. }
  33. /* Configure GPIO pin with specific GPIO function for PWM timer */
  34. s3c_gpio_cfgpin(bl_gpio_info->no, bl_gpio_info->func);
  35. return 0;
  36. }
  37. static void samsung_bl_exit(struct device *dev)
  38. {
  39. struct platform_device *timer_dev =
  40. container_of(dev->parent, struct platform_device, dev);
  41. struct samsung_bl_gpio_info *bl_gpio_info =
  42. timer_dev->dev.platform_data;
  43. s3c_gpio_cfgpin(bl_gpio_info->no, S3C_GPIO_OUTPUT);
  44. gpio_free(bl_gpio_info->no);
  45. }
  46. /* Initialize few important fields of platform_pwm_backlight_data
  47. * structure with default values. These fields can be overridden by
  48. * board-specific values sent from machine file.
  49. * For ease of operation, these fields are initialized with values
  50. * used by most samsung boards.
  51. * Users has the option of sending info about other parameters
  52. * for their specific boards
  53. */
  54. static struct platform_pwm_backlight_data samsung_dfl_bl_data __initdata = {
  55. .max_brightness = 255,
  56. .dft_brightness = 255,
  57. .pwm_period_ns = 78770,
  58. .init = samsung_bl_init,
  59. .exit = samsung_bl_exit,
  60. };
  61. static struct platform_device samsung_dfl_bl_device __initdata = {
  62. .name = "pwm-backlight",
  63. };
  64. /* samsung_bl_set - Set board specific data (if any) provided by user for
  65. * PWM Backlight control and register specific PWM and backlight device.
  66. * @gpio_info: structure containing GPIO info for PWM timer
  67. * @bl_data: structure containing Backlight control data
  68. */
  69. void samsung_bl_set(struct samsung_bl_gpio_info *gpio_info,
  70. struct platform_pwm_backlight_data *bl_data)
  71. {
  72. int ret = 0;
  73. struct platform_device *samsung_bl_device;
  74. struct platform_pwm_backlight_data *samsung_bl_data;
  75. samsung_bl_device = kmemdup(&samsung_dfl_bl_device,
  76. sizeof(struct platform_device), GFP_KERNEL);
  77. if (!samsung_bl_device) {
  78. printk(KERN_ERR "%s: no memory for platform dev\n", __func__);
  79. return;
  80. }
  81. samsung_bl_data = s3c_set_platdata(&samsung_dfl_bl_data,
  82. sizeof(struct platform_pwm_backlight_data), samsung_bl_device);
  83. if (!samsung_bl_data) {
  84. printk(KERN_ERR "%s: no memory for platform dev\n", __func__);
  85. goto err_data;
  86. }
  87. /* Copy board specific data provided by user */
  88. samsung_bl_data->pwm_id = bl_data->pwm_id;
  89. samsung_bl_device->dev.parent =
  90. &s3c_device_timer[samsung_bl_data->pwm_id].dev;
  91. if (bl_data->max_brightness)
  92. samsung_bl_data->max_brightness = bl_data->max_brightness;
  93. if (bl_data->dft_brightness)
  94. samsung_bl_data->dft_brightness = bl_data->dft_brightness;
  95. if (bl_data->lth_brightness)
  96. samsung_bl_data->lth_brightness = bl_data->lth_brightness;
  97. if (bl_data->pwm_period_ns)
  98. samsung_bl_data->pwm_period_ns = bl_data->pwm_period_ns;
  99. if (bl_data->init)
  100. samsung_bl_data->init = bl_data->init;
  101. if (bl_data->notify)
  102. samsung_bl_data->notify = bl_data->notify;
  103. if (bl_data->exit)
  104. samsung_bl_data->exit = bl_data->exit;
  105. if (bl_data->check_fb)
  106. samsung_bl_data->check_fb = bl_data->check_fb;
  107. /* Keep the GPIO info for future use */
  108. s3c_device_timer[samsung_bl_data->pwm_id].dev.platform_data = gpio_info;
  109. /* Register the specific PWM timer dev for Backlight control */
  110. ret = platform_device_register(
  111. &s3c_device_timer[samsung_bl_data->pwm_id]);
  112. if (ret) {
  113. printk(KERN_ERR "failed to register pwm timer for backlight: %d\n", ret);
  114. goto err_plat_reg1;
  115. }
  116. /* Register the Backlight dev */
  117. ret = platform_device_register(samsung_bl_device);
  118. if (ret) {
  119. printk(KERN_ERR "failed to register backlight device: %d\n", ret);
  120. goto err_plat_reg2;
  121. }
  122. return;
  123. err_plat_reg2:
  124. platform_device_unregister(&s3c_device_timer[samsung_bl_data->pwm_id]);
  125. err_plat_reg1:
  126. kfree(samsung_bl_data);
  127. err_data:
  128. kfree(samsung_bl_device);
  129. return;
  130. }