summaryrefslogtreecommitdiff
path: root/drivers/video/backlight/mxs_bl.c
blob: 2e2e9a8ebad4e78ef97605a7039c9b9779b58cbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
 * Backlight Driver for Freescale MXS
 *
 * Embedded Alley Solutions, Inc <source@embeddedalley.com>
 *
 * Copyright 2008-2010 Freescale Semiconductor, Inc.
 * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/spinlock.h>
#include <linux/fb.h>
#include <linux/backlight.h>
#include <linux/clk.h>
#include <linux/io.h>
#include <linux/delay.h>
#include <linux/regulator/consumer.h>

#include <mach/lcdif.h>
#include <mach/regulator.h>

struct mxs_bl_data {
	struct notifier_block nb;
	struct notifier_block reg_nb;
	struct notifier_block reg_init_nb;
	struct backlight_device *bd;
	struct mxs_platform_bl_data *pdata;
	int current_intensity;
	int saved_intensity;
	int mxsbl_suspended;
	int mxsbl_constrained;
};

static int mxsbl_do_probe(struct mxs_bl_data *data,
		struct mxs_platform_bl_data *pdata);
static int mxsbl_set_intensity(struct backlight_device *bd);
static inline void bl_register_reg(struct mxs_platform_bl_data *pdata,
				   struct mxs_bl_data *data);


/*
 * If we got here init is done
 */
static int bl_init_reg_callback(struct notifier_block *self,
			unsigned long event, void *data)
{
	struct mxs_bl_data *bdata;
	struct mxs_platform_bl_data *pdata;
	struct regulator *r = regulator_get(NULL, "mxs-bl-1");

	bdata = container_of(self, struct mxs_bl_data, reg_init_nb);
	pdata = bdata->pdata;

	if (r && !IS_ERR(r))
		regulator_put(r);
	else
		goto out;

	bl_register_reg(pdata, bdata);

	if (pdata->regulator) {

		printk(KERN_NOTICE"%s: setting intensity\n", __func__);

		bus_unregister_notifier(&platform_bus_type,
					&bdata->reg_init_nb);
		mutex_lock(&bdata->bd->ops_lock);
		mxsbl_set_intensity(bdata->bd);
		mutex_unlock(&bdata->bd->ops_lock);
	}

out:
	return 0;
}

static int bl_reg_callback(struct notifier_block *self,
			unsigned long event, void *data)
{
	struct mxs_bl_data *bdata;
	struct mxs_platform_bl_data *pdata;
	bdata = container_of(self, struct mxs_bl_data, reg_nb);
	pdata = bdata->pdata;

	mutex_lock(&bdata->bd->ops_lock);

	switch (event) {
	case MXS_REG5V_IS_USB:
		bdata->bd->props.max_brightness = pdata->bl_cons_intensity;
		bdata->bd->props.brightness = pdata->bl_cons_intensity;
		bdata->saved_intensity = bdata->current_intensity;
		bdata->mxsbl_constrained = 1;
		break;
	case MXS_REG5V_NOT_USB:
		bdata->bd->props.max_brightness = pdata->bl_max_intensity;
		bdata->bd->props.brightness = bdata->saved_intensity;
		bdata->mxsbl_constrained = 0;
		break;
	}

	mxsbl_set_intensity(bdata->bd);
	mutex_unlock(&bdata->bd->ops_lock);
	return 0;
}

static inline void bl_unregister_reg(struct mxs_platform_bl_data *pdata,
				  struct mxs_bl_data *data)
{
	if (!pdata)
		return;
	if (pdata->regulator)
		regulator_unregister_notifier(pdata->regulator,
					    &data->reg_nb);
	if (pdata->regulator)
		regulator_put(pdata->regulator);
	pdata->regulator = NULL;
}

static inline void bl_register_reg(struct mxs_platform_bl_data *pdata,
				   struct mxs_bl_data *data)
{
	pdata->regulator = regulator_get(NULL, "mxs-bl-1");
	if (pdata->regulator && !IS_ERR(pdata->regulator)) {
		regulator_set_mode(pdata->regulator, REGULATOR_MODE_FAST);
		if (pdata->regulator) {
			data->reg_nb.notifier_call = bl_reg_callback;
			regulator_register_notifier(pdata->regulator,
						    &data->reg_nb);
		}
	} else{
		printk(KERN_ERR "%s: failed to get regulator\n", __func__);
		pdata->regulator = NULL;
	}

}

static int bl_callback(struct notifier_block *self,
		       unsigned long event, void *data)
{
	struct mxs_platform_fb_entry *pentry = data;
	struct mxs_bl_data *bdata;
	struct mxs_platform_bl_data *pdata;

	switch (event) {
	case MXS_LCDIF_PANEL_INIT:
		bdata = container_of(self, struct mxs_bl_data, nb);
		pdata = pentry->bl_data;
		bdata->pdata = pdata;
		if (pdata) {
			bl_register_reg(pdata, bdata);
			if (!pdata->regulator) {
				/* wait for regulator to appear */
				bdata->reg_init_nb.notifier_call =
						bl_init_reg_callback;
				bus_register_notifier(&platform_bus_type,
						      &bdata->reg_init_nb);
			}
			return mxsbl_do_probe(bdata, pdata);
		}
		break;

	case MXS_LCDIF_PANEL_RELEASE:
		bdata = container_of(self, struct mxs_bl_data, nb);
		pdata = pentry->bl_data;
		if (pdata) {
			bus_unregister_notifier(&platform_bus_type,
						&bdata->reg_init_nb);
			bl_unregister_reg(pdata, bdata);
			pdata->free_bl(pdata);
		}
		bdata->pdata = NULL;
		break;
	}
	return 0;
}

#ifdef CONFIG_PM
static int mxsbl_suspend(struct platform_device *pdev, pm_message_t state)
{
	struct mxs_bl_data *data = platform_get_drvdata(pdev);
	struct mxs_platform_bl_data *pdata = data->pdata;

	data->mxsbl_suspended = 1;
	if (pdata) {
		dev_dbg(&pdev->dev, "real suspend\n");
		mxsbl_set_intensity(data->bd);
	}
	return 0;
}

static int mxsbl_resume(struct platform_device *pdev)
{
	struct mxs_bl_data *data = platform_get_drvdata(pdev);
	struct mxs_platform_bl_data *pdata = data->pdata;
	int ret = 0;

	data->mxsbl_suspended = 0;
	if (pdata) {
		dev_dbg(&pdev->dev, "real resume\n");
		pdata->free_bl(pdata);
		ret = pdata->init_bl(pdata);
		if (ret)
			goto out;
		mxsbl_set_intensity(data->bd);
	}
out:
	return ret;
}
#else
#define mxsbl_suspend	NULL
#define mxsbl_resume	NULL
#endif
/*
 *  This function should be called with bd->ops_lock held
 *  Suspend/resume ?
 */
static int mxsbl_set_intensity(struct backlight_device *bd)
{
	struct platform_device *pdev = dev_get_drvdata(&bd->dev);
	struct mxs_bl_data *data = platform_get_drvdata(pdev);
	struct mxs_platform_bl_data *pdata = data->pdata;

	if (pdata) {
		int ret;

		ret = pdata->set_bl_intensity(pdata, bd,
					      data->mxsbl_suspended);
		if (ret)
			bd->props.brightness = data->current_intensity;
		else
			data->current_intensity = bd->props.brightness;
		return ret;
	} else
		return -ENODEV;
}

static int mxsbl_get_intensity(struct backlight_device *bd)
{
	struct platform_device *pdev = dev_get_drvdata(&bd->dev);
	struct mxs_bl_data *data = platform_get_drvdata(pdev);

	return data->current_intensity;
}

static struct backlight_ops mxsbl_ops = {
	.get_brightness = mxsbl_get_intensity,
	.update_status  = mxsbl_set_intensity,
};

static int mxsbl_do_probe(struct mxs_bl_data *data,
		struct mxs_platform_bl_data *pdata)
{
	int ret = pdata->init_bl(pdata);

	if (ret)
		goto out;

	data->bd->props.power = FB_BLANK_UNBLANK;
	data->bd->props.fb_blank = FB_BLANK_UNBLANK;
	if (data->mxsbl_constrained) {
		data->bd->props.max_brightness = pdata->bl_cons_intensity;
		data->bd->props.brightness = pdata->bl_cons_intensity;
	} else {
		data->bd->props.max_brightness = pdata->bl_max_intensity;
		data->bd->props.brightness = pdata->bl_default_intensity;
	}

	data->pdata = pdata;
	mxsbl_set_intensity(data->bd);

out:
	return ret;
}

static int __init mxsbl_probe(struct platform_device *pdev)
{
	struct mxs_bl_data *data;
	struct mxs_platform_bl_data *pdata = pdev->dev.platform_data;
	int ret = 0;

	data = kzalloc(sizeof(*data), GFP_KERNEL);
	if (!data) {
		ret = -ENOMEM;
		goto out;
	}
	data->bd = backlight_device_register(pdev->name, &pdev->dev, pdev,
					&mxsbl_ops);
	if (IS_ERR(data->bd)) {
		ret = PTR_ERR(data->bd);
		goto out_1;
	}

	get_device(&pdev->dev);

	data->nb.notifier_call = bl_callback;
	mxs_lcdif_register_client(&data->nb);
	platform_set_drvdata(pdev, data);

	if (pdata) {
		ret = mxsbl_do_probe(data, pdata);
		if (ret)
			goto out_2;
	}

	goto out;

out_2:
	put_device(&pdev->dev);
out_1:
	kfree(data);
out:
	return ret;
}

static int mxsbl_remove(struct platform_device *pdev)
{
	struct mxs_platform_bl_data *pdata = pdev->dev.platform_data;
	struct mxs_bl_data *data = platform_get_drvdata(pdev);
	struct backlight_device *bd = data->bd;

	bd->props.power = FB_BLANK_POWERDOWN;
	bd->props.fb_blank = FB_BLANK_POWERDOWN;
	bd->props.brightness = 0;
	data->current_intensity = bd->props.brightness;

	if (pdata) {
		pdata->set_bl_intensity(pdata, bd, data->mxsbl_suspended);
		if (pdata->free_bl)
			pdata->free_bl(pdata);
	}
	backlight_device_unregister(bd);
	if (pdata->regulator)
		regulator_put(pdata->regulator);
	put_device(&pdev->dev);
	platform_set_drvdata(pdev, NULL);
	mxs_lcdif_unregister_client(&data->nb);
	kfree(data);

	return 0;
}

static struct platform_driver mxsbl_driver = {
	.probe		= mxsbl_probe,
	.remove		= __devexit_p(mxsbl_remove),
	.suspend	= mxsbl_suspend,
	.resume		= mxsbl_resume,
	.driver		= {
		.name	= "mxs-bl",
		.owner	= THIS_MODULE,
	},
};

static int __init mxs_init(void)
{
	return platform_driver_register(&mxsbl_driver);
}

static void __exit mxs_exit(void)
{
	platform_driver_unregister(&mxsbl_driver);
}

module_init(mxs_init);
module_exit(mxs_exit);

MODULE_AUTHOR("Embedded Alley Solutions, Inc <sources@embeddedalley.com>");
MODULE_DESCRIPTION("MXS Backlight Driver");
MODULE_LICENSE("GPL");