dev-backlight.c 4.5 KB

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