perf record: Fix segfault with --no-mmap-pages
[linux/fpc-iii.git] / drivers / video / backlight / atmel-pwm-bl.c
blob0393d827dd44bcfa6f472d78c11823892b944af3
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>
20 #include <linux/slab.h>
22 struct 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;
30 static int atmel_pwm_bl_set_intensity(struct backlight_device *bd)
32 struct atmel_pwm_bl *pwmbl = bl_get_data(bd);
33 int intensity = bd->props.brightness;
34 int pwm_duty;
36 if (bd->props.power != FB_BLANK_UNBLANK)
37 intensity = 0;
38 if (bd->props.fb_blank != FB_BLANK_UNBLANK)
39 intensity = 0;
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;
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;
51 if (!intensity) {
52 if (pwmbl->gpio_on != -1) {
53 gpio_set_value(pwmbl->gpio_on,
54 0 ^ pwmbl->pdata->on_active_low);
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);
67 return 0;
70 static int atmel_pwm_bl_get_intensity(struct backlight_device *bd)
72 struct atmel_pwm_bl *pwmbl = bl_get_data(bd);
73 u8 intensity;
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);
83 return intensity;
86 static int atmel_pwm_bl_init_pwm(struct atmel_pwm_bl *pwmbl)
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;
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).
98 prescale = fls(prescale);
99 if (prescale > 0xf)
100 prescale = 0xf;
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);
109 dev_info(&pwmbl->pdev->dev, "Atmel PWM backlight driver (%lu Hz)\n",
110 pwmbl->pwmc.mck / 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 __init 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 = devm_kzalloc(&pdev->dev, sizeof(struct atmel_pwm_bl),
130 GFP_KERNEL);
131 if (!pwmbl)
132 return -ENOMEM;
134 pwmbl->pdev = pdev;
136 pdata = pdev->dev.platform_data;
137 if (!pdata) {
138 retval = -ENODEV;
139 goto err_free_mem;
142 if (pdata->pwm_compare_max < pdata->pwm_duty_max ||
143 pdata->pwm_duty_min > pdata->pwm_duty_max ||
144 pdata->pwm_frequency == 0) {
145 retval = -EINVAL;
146 goto err_free_mem;
149 pwmbl->pdata = pdata;
150 pwmbl->gpio_on = pdata->gpio_on;
152 retval = pwm_channel_alloc(pdata->pwm_channel, &pwmbl->pwmc);
153 if (retval)
154 goto err_free_mem;
156 if (pwmbl->gpio_on != -1) {
157 retval = devm_gpio_request(&pdev->dev, pwmbl->gpio_on,
158 "gpio_atmel_pwm_bl");
159 if (retval) {
160 pwmbl->gpio_on = -1;
161 goto err_free_pwm;
164 /* Turn display off by default. */
165 retval = gpio_direction_output(pwmbl->gpio_on,
166 0 ^ pdata->on_active_low);
167 if (retval)
168 goto err_free_pwm;
171 memset(&props, 0, sizeof(struct backlight_properties));
172 props.type = BACKLIGHT_RAW;
173 props.max_brightness = pdata->pwm_duty_max - pdata->pwm_duty_min;
174 bldev = backlight_device_register("atmel-pwm-bl", &pdev->dev, pwmbl,
175 &atmel_pwm_bl_ops, &props);
176 if (IS_ERR(bldev)) {
177 retval = PTR_ERR(bldev);
178 goto err_free_pwm;
181 pwmbl->bldev = bldev;
183 platform_set_drvdata(pdev, pwmbl);
185 /* Power up the backlight by default at middle intesity. */
186 bldev->props.power = FB_BLANK_UNBLANK;
187 bldev->props.brightness = bldev->props.max_brightness / 2;
189 retval = atmel_pwm_bl_init_pwm(pwmbl);
190 if (retval)
191 goto err_free_bl_dev;
193 atmel_pwm_bl_set_intensity(bldev);
195 return 0;
197 err_free_bl_dev:
198 backlight_device_unregister(bldev);
199 err_free_pwm:
200 pwm_channel_free(&pwmbl->pwmc);
201 err_free_mem:
202 return retval;
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 pwm_channel_disable(&pwmbl->pwmc);
212 pwm_channel_free(&pwmbl->pwmc);
213 backlight_device_unregister(pwmbl->bldev);
215 return 0;
218 static struct platform_driver atmel_pwm_bl_driver = {
219 .driver = {
220 .name = "atmel-pwm-bl",
222 /* REVISIT add suspend() and resume() */
223 .remove = __exit_p(atmel_pwm_bl_remove),
226 module_platform_driver_probe(atmel_pwm_bl_driver, atmel_pwm_bl_probe);
228 MODULE_AUTHOR("Hans-Christian egtvedt <hans-christian.egtvedt@atmel.com>");
229 MODULE_DESCRIPTION("Atmel PWM backlight driver");
230 MODULE_LICENSE("GPL");