Linux 2.6.34-rc3
[pohmelfs.git] / drivers / video / backlight / atmel-pwm-bl.c
blob2d9760551a4b738e1d0ebaef4ffed32f2a4fdf2f
1 /*
2 * Copyright (C) 2008 Atmel Corporation
4 * Backlight driver using Atmel PWM peripheral.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 */
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/fb.h>
15 #include <linux/clk.h>
16 #include <linux/gpio.h>
17 #include <linux/backlight.h>
18 #include <linux/atmel_pwm.h>
19 #include <linux/atmel-pwm-bl.h>
21 struct atmel_pwm_bl {
22 const struct atmel_pwm_bl_platform_data *pdata;
23 struct backlight_device *bldev;
24 struct platform_device *pdev;
25 struct pwm_channel pwmc;
26 int gpio_on;
29 static int atmel_pwm_bl_set_intensity(struct backlight_device *bd)
31 struct atmel_pwm_bl *pwmbl = bl_get_data(bd);
32 int intensity = bd->props.brightness;
33 int pwm_duty;
35 if (bd->props.power != FB_BLANK_UNBLANK)
36 intensity = 0;
37 if (bd->props.fb_blank != FB_BLANK_UNBLANK)
38 intensity = 0;
40 if (pwmbl->pdata->pwm_active_low)
41 pwm_duty = pwmbl->pdata->pwm_duty_min + intensity;
42 else
43 pwm_duty = pwmbl->pdata->pwm_duty_max - intensity;
45 if (pwm_duty > pwmbl->pdata->pwm_duty_max)
46 pwm_duty = pwmbl->pdata->pwm_duty_max;
47 if (pwm_duty < pwmbl->pdata->pwm_duty_min)
48 pwm_duty = pwmbl->pdata->pwm_duty_min;
50 if (!intensity) {
51 if (pwmbl->gpio_on != -1) {
52 gpio_set_value(pwmbl->gpio_on,
53 0 ^ pwmbl->pdata->on_active_low);
55 pwm_channel_writel(&pwmbl->pwmc, PWM_CUPD, pwm_duty);
56 pwm_channel_disable(&pwmbl->pwmc);
57 } else {
58 pwm_channel_enable(&pwmbl->pwmc);
59 pwm_channel_writel(&pwmbl->pwmc, PWM_CUPD, pwm_duty);
60 if (pwmbl->gpio_on != -1) {
61 gpio_set_value(pwmbl->gpio_on,
62 1 ^ pwmbl->pdata->on_active_low);
66 return 0;
69 static int atmel_pwm_bl_get_intensity(struct backlight_device *bd)
71 struct atmel_pwm_bl *pwmbl = bl_get_data(bd);
72 u8 intensity;
74 if (pwmbl->pdata->pwm_active_low) {
75 intensity = pwm_channel_readl(&pwmbl->pwmc, PWM_CDTY) -
76 pwmbl->pdata->pwm_duty_min;
77 } else {
78 intensity = pwmbl->pdata->pwm_duty_max -
79 pwm_channel_readl(&pwmbl->pwmc, PWM_CDTY);
82 return intensity;
85 static int atmel_pwm_bl_init_pwm(struct atmel_pwm_bl *pwmbl)
87 unsigned long pwm_rate = pwmbl->pwmc.mck;
88 unsigned long prescale = DIV_ROUND_UP(pwm_rate,
89 (pwmbl->pdata->pwm_frequency *
90 pwmbl->pdata->pwm_compare_max)) - 1;
93 * Prescale must be power of two and maximum 0xf in size because of
94 * hardware limit. PWM speed will be:
95 * PWM module clock speed / (2 ^ prescale).
97 prescale = fls(prescale);
98 if (prescale > 0xf)
99 prescale = 0xf;
101 pwm_channel_writel(&pwmbl->pwmc, PWM_CMR, prescale);
102 pwm_channel_writel(&pwmbl->pwmc, PWM_CDTY,
103 pwmbl->pdata->pwm_duty_min +
104 pwmbl->bldev->props.brightness);
105 pwm_channel_writel(&pwmbl->pwmc, PWM_CPRD,
106 pwmbl->pdata->pwm_compare_max);
108 dev_info(&pwmbl->pdev->dev, "Atmel PWM backlight driver "
109 "(%lu Hz)\n", pwmbl->pwmc.mck /
110 pwmbl->pdata->pwm_compare_max /
111 (1 << prescale));
113 return pwm_channel_enable(&pwmbl->pwmc);
116 static const struct backlight_ops atmel_pwm_bl_ops = {
117 .get_brightness = atmel_pwm_bl_get_intensity,
118 .update_status = atmel_pwm_bl_set_intensity,
121 static int atmel_pwm_bl_probe(struct platform_device *pdev)
123 struct backlight_properties props;
124 const struct atmel_pwm_bl_platform_data *pdata;
125 struct backlight_device *bldev;
126 struct atmel_pwm_bl *pwmbl;
127 int retval;
129 pwmbl = kzalloc(sizeof(struct atmel_pwm_bl), GFP_KERNEL);
130 if (!pwmbl)
131 return -ENOMEM;
133 pwmbl->pdev = pdev;
135 pdata = pdev->dev.platform_data;
136 if (!pdata) {
137 retval = -ENODEV;
138 goto err_free_mem;
141 if (pdata->pwm_compare_max < pdata->pwm_duty_max ||
142 pdata->pwm_duty_min > pdata->pwm_duty_max ||
143 pdata->pwm_frequency == 0) {
144 retval = -EINVAL;
145 goto err_free_mem;
148 pwmbl->pdata = pdata;
149 pwmbl->gpio_on = pdata->gpio_on;
151 retval = pwm_channel_alloc(pdata->pwm_channel, &pwmbl->pwmc);
152 if (retval)
153 goto err_free_mem;
155 if (pwmbl->gpio_on != -1) {
156 retval = gpio_request(pwmbl->gpio_on, "gpio_atmel_pwm_bl");
157 if (retval) {
158 pwmbl->gpio_on = -1;
159 goto err_free_pwm;
162 /* Turn display off by default. */
163 retval = gpio_direction_output(pwmbl->gpio_on,
164 0 ^ pdata->on_active_low);
165 if (retval)
166 goto err_free_gpio;
169 memset(&props, 0, sizeof(struct backlight_properties));
170 props.max_brightness = pdata->pwm_duty_max - pdata->pwm_duty_min;
171 bldev = backlight_device_register("atmel-pwm-bl", &pdev->dev, pwmbl,
172 &atmel_pwm_bl_ops, &props);
173 if (IS_ERR(bldev)) {
174 retval = PTR_ERR(bldev);
175 goto err_free_gpio;
178 pwmbl->bldev = bldev;
180 platform_set_drvdata(pdev, pwmbl);
182 /* Power up the backlight by default at middle intesity. */
183 bldev->props.power = FB_BLANK_UNBLANK;
184 bldev->props.brightness = bldev->props.max_brightness / 2;
186 retval = atmel_pwm_bl_init_pwm(pwmbl);
187 if (retval)
188 goto err_free_bl_dev;
190 atmel_pwm_bl_set_intensity(bldev);
192 return 0;
194 err_free_bl_dev:
195 platform_set_drvdata(pdev, NULL);
196 backlight_device_unregister(bldev);
197 err_free_gpio:
198 if (pwmbl->gpio_on != -1)
199 gpio_free(pwmbl->gpio_on);
200 err_free_pwm:
201 pwm_channel_free(&pwmbl->pwmc);
202 err_free_mem:
203 kfree(pwmbl);
204 return retval;
207 static int __exit atmel_pwm_bl_remove(struct platform_device *pdev)
209 struct atmel_pwm_bl *pwmbl = platform_get_drvdata(pdev);
211 if (pwmbl->gpio_on != -1) {
212 gpio_set_value(pwmbl->gpio_on, 0);
213 gpio_free(pwmbl->gpio_on);
215 pwm_channel_disable(&pwmbl->pwmc);
216 pwm_channel_free(&pwmbl->pwmc);
217 backlight_device_unregister(pwmbl->bldev);
218 platform_set_drvdata(pdev, NULL);
219 kfree(pwmbl);
221 return 0;
224 static struct platform_driver atmel_pwm_bl_driver = {
225 .driver = {
226 .name = "atmel-pwm-bl",
228 /* REVISIT add suspend() and resume() */
229 .remove = __exit_p(atmel_pwm_bl_remove),
232 static int __init atmel_pwm_bl_init(void)
234 return platform_driver_probe(&atmel_pwm_bl_driver, atmel_pwm_bl_probe);
236 module_init(atmel_pwm_bl_init);
238 static void __exit atmel_pwm_bl_exit(void)
240 platform_driver_unregister(&atmel_pwm_bl_driver);
242 module_exit(atmel_pwm_bl_exit);
244 MODULE_AUTHOR("Hans-Christian egtvedt <hans-christian.egtvedt@atmel.com>");
245 MODULE_DESCRIPTION("Atmel PWM backlight driver");
246 MODULE_LICENSE("GPL");