blob: df1cbb7ef6ca0370551904b190ba968315ee92c7 [file] [log] [blame]
Hans-Christian Egtvedt3e074052008-07-23 21:31:48 -07001/*
2 * Copyright (C) 2008 Atmel Corporation
3 *
4 * Backlight driver using Atmel PWM peripheral.
5 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Hans-Christian Egtvedt3e074052008-07-23 21:31:48 -070021
22struct atmel_pwm_bl {
23 const struct atmel_pwm_bl_platform_data *pdata;
24 struct backlight_device *bldev;
25 struct platform_device *pdev;
26 struct pwm_channel pwmc;
27 int gpio_on;
28};
29
30static int atmel_pwm_bl_set_intensity(struct backlight_device *bd)
31{
32 struct atmel_pwm_bl *pwmbl = bl_get_data(bd);
33 int intensity = bd->props.brightness;
34 int pwm_duty;
35
36 if (bd->props.power != FB_BLANK_UNBLANK)
37 intensity = 0;
38 if (bd->props.fb_blank != FB_BLANK_UNBLANK)
39 intensity = 0;
40
41 if (pwmbl->pdata->pwm_active_low)
42 pwm_duty = pwmbl->pdata->pwm_duty_min + intensity;
43 else
44 pwm_duty = pwmbl->pdata->pwm_duty_max - intensity;
45
46 if (pwm_duty > pwmbl->pdata->pwm_duty_max)
47 pwm_duty = pwmbl->pdata->pwm_duty_max;
48 if (pwm_duty < pwmbl->pdata->pwm_duty_min)
49 pwm_duty = pwmbl->pdata->pwm_duty_min;
50
51 if (!intensity) {
52 if (pwmbl->gpio_on != -1) {
53 gpio_set_value(pwmbl->gpio_on,
54 0 ^ pwmbl->pdata->on_active_low);
55 }
56 pwm_channel_writel(&pwmbl->pwmc, PWM_CUPD, pwm_duty);
57 pwm_channel_disable(&pwmbl->pwmc);
58 } else {
59 pwm_channel_enable(&pwmbl->pwmc);
60 pwm_channel_writel(&pwmbl->pwmc, PWM_CUPD, pwm_duty);
61 if (pwmbl->gpio_on != -1) {
62 gpio_set_value(pwmbl->gpio_on,
63 1 ^ pwmbl->pdata->on_active_low);
64 }
65 }
66
67 return 0;
68}
69
70static int atmel_pwm_bl_get_intensity(struct backlight_device *bd)
71{
72 struct atmel_pwm_bl *pwmbl = bl_get_data(bd);
73 u8 intensity;
74
75 if (pwmbl->pdata->pwm_active_low) {
76 intensity = pwm_channel_readl(&pwmbl->pwmc, PWM_CDTY) -
77 pwmbl->pdata->pwm_duty_min;
78 } else {
79 intensity = pwmbl->pdata->pwm_duty_max -
80 pwm_channel_readl(&pwmbl->pwmc, PWM_CDTY);
81 }
82
83 return intensity;
84}
85
86static int atmel_pwm_bl_init_pwm(struct atmel_pwm_bl *pwmbl)
87{
88 unsigned long pwm_rate = pwmbl->pwmc.mck;
89 unsigned long prescale = DIV_ROUND_UP(pwm_rate,
90 (pwmbl->pdata->pwm_frequency *
91 pwmbl->pdata->pwm_compare_max)) - 1;
92
93 /*
94 * Prescale must be power of two and maximum 0xf in size because of
95 * hardware limit. PWM speed will be:
96 * PWM module clock speed / (2 ^ prescale).
97 */
98 prescale = fls(prescale);
99 if (prescale > 0xf)
100 prescale = 0xf;
101
102 pwm_channel_writel(&pwmbl->pwmc, PWM_CMR, prescale);
103 pwm_channel_writel(&pwmbl->pwmc, PWM_CDTY,
104 pwmbl->pdata->pwm_duty_min +
105 pwmbl->bldev->props.brightness);
106 pwm_channel_writel(&pwmbl->pwmc, PWM_CPRD,
107 pwmbl->pdata->pwm_compare_max);
108
109 dev_info(&pwmbl->pdev->dev, "Atmel PWM backlight driver "
110 "(%lu Hz)\n", pwmbl->pwmc.mck /
111 pwmbl->pdata->pwm_compare_max /
112 (1 << prescale));
113
114 return pwm_channel_enable(&pwmbl->pwmc);
115}
116
Emese Revfy9905a432009-12-14 00:58:57 +0100117static const struct backlight_ops atmel_pwm_bl_ops = {
Hans-Christian Egtvedt3e074052008-07-23 21:31:48 -0700118 .get_brightness = atmel_pwm_bl_get_intensity,
119 .update_status = atmel_pwm_bl_set_intensity,
120};
121
122static int atmel_pwm_bl_probe(struct platform_device *pdev)
123{
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500124 struct backlight_properties props;
Hans-Christian Egtvedt3e074052008-07-23 21:31:48 -0700125 const struct atmel_pwm_bl_platform_data *pdata;
126 struct backlight_device *bldev;
127 struct atmel_pwm_bl *pwmbl;
128 int retval;
129
Jingoo Hana4be29a2012-07-30 14:40:31 -0700130 pwmbl = devm_kzalloc(&pdev->dev, sizeof(struct atmel_pwm_bl),
131 GFP_KERNEL);
Hans-Christian Egtvedt3e074052008-07-23 21:31:48 -0700132 if (!pwmbl)
133 return -ENOMEM;
134
135 pwmbl->pdev = pdev;
136
137 pdata = pdev->dev.platform_data;
138 if (!pdata) {
139 retval = -ENODEV;
140 goto err_free_mem;
141 }
142
143 if (pdata->pwm_compare_max < pdata->pwm_duty_max ||
144 pdata->pwm_duty_min > pdata->pwm_duty_max ||
145 pdata->pwm_frequency == 0) {
146 retval = -EINVAL;
147 goto err_free_mem;
148 }
149
150 pwmbl->pdata = pdata;
151 pwmbl->gpio_on = pdata->gpio_on;
152
153 retval = pwm_channel_alloc(pdata->pwm_channel, &pwmbl->pwmc);
154 if (retval)
155 goto err_free_mem;
156
157 if (pwmbl->gpio_on != -1) {
Jingoo Han5f9dd5c2012-07-30 14:40:36 -0700158 retval = devm_gpio_request(&pdev->dev, pwmbl->gpio_on,
159 "gpio_atmel_pwm_bl");
Hans-Christian Egtvedt3e074052008-07-23 21:31:48 -0700160 if (retval) {
161 pwmbl->gpio_on = -1;
162 goto err_free_pwm;
163 }
164
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200165 /* Turn display off by default. */
Hans-Christian Egtvedt3e074052008-07-23 21:31:48 -0700166 retval = gpio_direction_output(pwmbl->gpio_on,
167 0 ^ pdata->on_active_low);
168 if (retval)
Jingoo Han5f9dd5c2012-07-30 14:40:36 -0700169 goto err_free_pwm;
Hans-Christian Egtvedt3e074052008-07-23 21:31:48 -0700170 }
171
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500172 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700173 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500174 props.max_brightness = pdata->pwm_duty_max - pdata->pwm_duty_min;
175 bldev = backlight_device_register("atmel-pwm-bl", &pdev->dev, pwmbl,
176 &atmel_pwm_bl_ops, &props);
Hans-Christian Egtvedt3e074052008-07-23 21:31:48 -0700177 if (IS_ERR(bldev)) {
178 retval = PTR_ERR(bldev);
Jingoo Han5f9dd5c2012-07-30 14:40:36 -0700179 goto err_free_pwm;
Hans-Christian Egtvedt3e074052008-07-23 21:31:48 -0700180 }
181
182 pwmbl->bldev = bldev;
183
184 platform_set_drvdata(pdev, pwmbl);
185
186 /* Power up the backlight by default at middle intesity. */
187 bldev->props.power = FB_BLANK_UNBLANK;
Hans-Christian Egtvedt3e074052008-07-23 21:31:48 -0700188 bldev->props.brightness = bldev->props.max_brightness / 2;
189
190 retval = atmel_pwm_bl_init_pwm(pwmbl);
191 if (retval)
192 goto err_free_bl_dev;
193
194 atmel_pwm_bl_set_intensity(bldev);
195
196 return 0;
197
198err_free_bl_dev:
199 platform_set_drvdata(pdev, NULL);
200 backlight_device_unregister(bldev);
Hans-Christian Egtvedt3e074052008-07-23 21:31:48 -0700201err_free_pwm:
202 pwm_channel_free(&pwmbl->pwmc);
203err_free_mem:
Hans-Christian Egtvedt3e074052008-07-23 21:31:48 -0700204 return retval;
205}
206
207static int __exit atmel_pwm_bl_remove(struct platform_device *pdev)
208{
209 struct atmel_pwm_bl *pwmbl = platform_get_drvdata(pdev);
210
Jingoo Han5f9dd5c2012-07-30 14:40:36 -0700211 if (pwmbl->gpio_on != -1)
Hans-Christian Egtvedt3e074052008-07-23 21:31:48 -0700212 gpio_set_value(pwmbl->gpio_on, 0);
Hans-Christian Egtvedt3e074052008-07-23 21:31:48 -0700213 pwm_channel_disable(&pwmbl->pwmc);
214 pwm_channel_free(&pwmbl->pwmc);
215 backlight_device_unregister(pwmbl->bldev);
216 platform_set_drvdata(pdev, NULL);
Hans-Christian Egtvedt3e074052008-07-23 21:31:48 -0700217
218 return 0;
219}
220
221static struct platform_driver atmel_pwm_bl_driver = {
222 .driver = {
223 .name = "atmel-pwm-bl",
224 },
225 /* REVISIT add suspend() and resume() */
226 .remove = __exit_p(atmel_pwm_bl_remove),
227};
228
229static int __init atmel_pwm_bl_init(void)
230{
231 return platform_driver_probe(&atmel_pwm_bl_driver, atmel_pwm_bl_probe);
232}
233module_init(atmel_pwm_bl_init);
234
235static void __exit atmel_pwm_bl_exit(void)
236{
237 platform_driver_unregister(&atmel_pwm_bl_driver);
238}
239module_exit(atmel_pwm_bl_exit);
240
241MODULE_AUTHOR("Hans-Christian egtvedt <hans-christian.egtvedt@atmel.com>");
242MODULE_DESCRIPTION("Atmel PWM backlight driver");
243MODULE_LICENSE("GPL");