Merge branch 'next' of git://git.infradead.org/users/vkoul/slave-dma
[pandora-kernel.git] / arch / arm / plat-samsung / dev-backlight.c
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
13 #include <linux/gpio.h>
14 #include <linux/platform_device.h>
15 #include <linux/io.h>
16 #include <linux/pwm_backlight.h>
17
18 #include <plat/devs.h>
19 #include <plat/gpio-cfg.h>
20 #include <plat/backlight.h>
21
22 static int samsung_bl_init(struct device *dev)
23 {
24         int ret = 0;
25         struct platform_device *timer_dev =
26                         container_of(dev->parent, struct platform_device, dev);
27         struct samsung_bl_gpio_info *bl_gpio_info =
28                         timer_dev->dev.platform_data;
29
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
36         /* Configure GPIO pin with specific GPIO function for PWM timer */
37         s3c_gpio_cfgpin(bl_gpio_info->no, bl_gpio_info->func);
38
39         return 0;
40 }
41
42 static void samsung_bl_exit(struct device *dev)
43 {
44         struct platform_device *timer_dev =
45                         container_of(dev->parent, struct platform_device, dev);
46         struct samsung_bl_gpio_info *bl_gpio_info =
47                         timer_dev->dev.platform_data;
48
49         s3c_gpio_cfgpin(bl_gpio_info->no, S3C_GPIO_OUTPUT);
50         gpio_free(bl_gpio_info->no);
51 }
52
53 /* Initialize few important fields of platform_pwm_backlight_data
54  * structure with default values. These fields can be overridden by
55  * board-specific values sent from machine file.
56  * For ease of operation, these fields are initialized with values
57  * used by most samsung boards.
58  * Users has the option of sending info about other parameters
59  * for their specific boards
60  */
61
62 static struct platform_pwm_backlight_data samsung_dfl_bl_data __initdata = {
63         .max_brightness = 255,
64         .dft_brightness = 255,
65         .pwm_period_ns  = 78770,
66         .init           = samsung_bl_init,
67         .exit           = samsung_bl_exit,
68 };
69
70 static struct platform_device samsung_dfl_bl_device __initdata = {
71         .name           = "pwm-backlight",
72 };
73
74 /* samsung_bl_set - Set board specific data (if any) provided by user for
75  * PWM Backlight control and register specific PWM and backlight device.
76  * @gpio_info:  structure containing GPIO info for PWM timer
77  * @bl_data:    structure containing Backlight control data
78  */
79 void samsung_bl_set(struct samsung_bl_gpio_info *gpio_info,
80         struct platform_pwm_backlight_data *bl_data)
81 {
82         int ret = 0;
83         struct platform_device *samsung_bl_device;
84         struct platform_pwm_backlight_data *samsung_bl_data;
85
86         samsung_bl_device = kmemdup(&samsung_dfl_bl_device,
87                         sizeof(struct platform_device), GFP_KERNEL);
88         if (!samsung_bl_device) {
89                 printk(KERN_ERR "%s: no memory for platform dev\n", __func__);
90                 return;
91         }
92
93         samsung_bl_data = s3c_set_platdata(&samsung_dfl_bl_data,
94                 sizeof(struct platform_pwm_backlight_data), samsung_bl_device);
95         if (!samsung_bl_data) {
96                 printk(KERN_ERR "%s: no memory for platform dev\n", __func__);
97                 goto err_data;
98         }
99
100         /* Copy board specific data provided by user */
101         samsung_bl_data->pwm_id = bl_data->pwm_id;
102         samsung_bl_device->dev.parent =
103                         &s3c_device_timer[samsung_bl_data->pwm_id].dev;
104
105         if (bl_data->max_brightness)
106                 samsung_bl_data->max_brightness = bl_data->max_brightness;
107         if (bl_data->dft_brightness)
108                 samsung_bl_data->dft_brightness = bl_data->dft_brightness;
109         if (bl_data->lth_brightness)
110                 samsung_bl_data->lth_brightness = bl_data->lth_brightness;
111         if (bl_data->pwm_period_ns)
112                 samsung_bl_data->pwm_period_ns = bl_data->pwm_period_ns;
113         if (bl_data->init)
114                 samsung_bl_data->init = bl_data->init;
115         if (bl_data->notify)
116                 samsung_bl_data->notify = bl_data->notify;
117         if (bl_data->exit)
118                 samsung_bl_data->exit = bl_data->exit;
119         if (bl_data->check_fb)
120                 samsung_bl_data->check_fb = bl_data->check_fb;
121
122         /* Keep the GPIO info for future use */
123         s3c_device_timer[samsung_bl_data->pwm_id].dev.platform_data = gpio_info;
124
125         /* Register the specific PWM timer dev for Backlight control */
126         ret = platform_device_register(
127                         &s3c_device_timer[samsung_bl_data->pwm_id]);
128         if (ret) {
129                 printk(KERN_ERR "failed to register pwm timer for backlight: %d\n", ret);
130                 goto err_plat_reg1;
131         }
132
133         /* Register the Backlight dev */
134         ret = platform_device_register(samsung_bl_device);
135         if (ret) {
136                 printk(KERN_ERR "failed to register backlight device: %d\n", ret);
137                 goto err_plat_reg2;
138         }
139
140         return;
141
142 err_plat_reg2:
143         platform_device_unregister(&s3c_device_timer[samsung_bl_data->pwm_id]);
144 err_plat_reg1:
145         kfree(samsung_bl_data);
146 err_data:
147         kfree(samsung_bl_device);
148         return;
149 }