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.
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.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>
22 const struct atmel_pwm_bl_platform_data *pdata;
23 struct backlight_device *bldev;
24 struct platform_device *pdev;
25 struct pwm_channel pwmc;
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;
35 if (bd->props.power != FB_BLANK_UNBLANK)
37 if (bd->props.fb_blank != FB_BLANK_UNBLANK)
40 if (pwmbl->pdata->pwm_active_low)
41 pwm_duty = pwmbl->pdata->pwm_duty_min + intensity;
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;
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);
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);
69 static int atmel_pwm_bl_get_intensity(struct backlight_device *bd)
71 struct atmel_pwm_bl *pwmbl = bl_get_data(bd);
74 if (pwmbl->pdata->pwm_active_low) {
75 intensity = pwm_channel_readl(&pwmbl->pwmc, PWM_CDTY) -
76 pwmbl->pdata->pwm_duty_min;
78 intensity = pwmbl->pdata->pwm_duty_max -
79 pwm_channel_readl(&pwmbl->pwmc, PWM_CDTY);
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);
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 /
113 return pwm_channel_enable(&pwmbl->pwmc);
116 static 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 const struct atmel_pwm_bl_platform_data *pdata;
124 struct backlight_device *bldev;
125 struct atmel_pwm_bl *pwmbl;
128 pwmbl = kzalloc(sizeof(struct atmel_pwm_bl), GFP_KERNEL);
134 pdata = pdev->dev.platform_data;
140 if (pdata->pwm_compare_max < pdata->pwm_duty_max ||
141 pdata->pwm_duty_min > pdata->pwm_duty_max ||
142 pdata->pwm_frequency == 0) {
147 pwmbl->pdata = pdata;
148 pwmbl->gpio_on = pdata->gpio_on;
150 retval = pwm_channel_alloc(pdata->pwm_channel, &pwmbl->pwmc);
154 if (pwmbl->gpio_on != -1) {
155 retval = gpio_request(pwmbl->gpio_on, "gpio_atmel_pwm_bl");
161 /* Turn display off by defatult. */
162 retval = gpio_direction_output(pwmbl->gpio_on,
163 0 ^ pdata->on_active_low);
168 bldev = backlight_device_register("atmel-pwm-bl",
169 &pdev->dev, pwmbl, &atmel_pwm_bl_ops);
171 retval = PTR_ERR(bldev);
175 pwmbl->bldev = bldev;
177 platform_set_drvdata(pdev, pwmbl);
179 /* Power up the backlight by default at middle intesity. */
180 bldev->props.power = FB_BLANK_UNBLANK;
181 bldev->props.max_brightness = pdata->pwm_duty_max - pdata->pwm_duty_min;
182 bldev->props.brightness = bldev->props.max_brightness / 2;
184 retval = atmel_pwm_bl_init_pwm(pwmbl);
186 goto err_free_bl_dev;
188 atmel_pwm_bl_set_intensity(bldev);
193 platform_set_drvdata(pdev, NULL);
194 backlight_device_unregister(bldev);
196 if (pwmbl->gpio_on != -1)
197 gpio_free(pwmbl->gpio_on);
199 pwm_channel_free(&pwmbl->pwmc);
205 static int __exit atmel_pwm_bl_remove(struct platform_device *pdev)
207 struct atmel_pwm_bl *pwmbl = platform_get_drvdata(pdev);
209 if (pwmbl->gpio_on != -1) {
210 gpio_set_value(pwmbl->gpio_on, 0);
211 gpio_free(pwmbl->gpio_on);
213 pwm_channel_disable(&pwmbl->pwmc);
214 pwm_channel_free(&pwmbl->pwmc);
215 backlight_device_unregister(pwmbl->bldev);
216 platform_set_drvdata(pdev, NULL);
222 static struct platform_driver atmel_pwm_bl_driver = {
224 .name = "atmel-pwm-bl",
226 /* REVISIT add suspend() and resume() */
227 .remove = __exit_p(atmel_pwm_bl_remove),
230 static int __init atmel_pwm_bl_init(void)
232 return platform_driver_probe(&atmel_pwm_bl_driver, atmel_pwm_bl_probe);
234 module_init(atmel_pwm_bl_init);
236 static void __exit atmel_pwm_bl_exit(void)
238 platform_driver_unregister(&atmel_pwm_bl_driver);
240 module_exit(atmel_pwm_bl_exit);
242 MODULE_AUTHOR("Hans-Christian egtvedt <hans-christian.egtvedt@atmel.com>");
243 MODULE_DESCRIPTION("Atmel PWM backlight driver");
244 MODULE_LICENSE("GPL");