aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/devfreq/exynos4_display.c
blob: c26124dee93de4abbcb0a4896d4a324b4c63ca94 (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
385
386
387
388
/* drivers/devfreq/exynos4_display.c
 *
 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com/
 *
 *	Chanwoo Choi <cw00.choi@samsung.com>
 *	Myungjoo Ham <myungjoo.ham@samsung.com>
 *	Kyungmin Park <kyungmin.park@samsung.com>
 *
 * EXYNOS4 - Dynamic LCD refresh rate support
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

#include <linux/kernel.h>
#include <linux/err.h>
#include <linux/opp.h>
#include <linux/mutex.h>
#include <linux/suspend.h>
#include <linux/notifier.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/devfreq.h>
#include <linux/devfreq/exynos4_display.h>
#include <linux/pm_qos_params.h>

#define EXYNOS4_DISPLAY_ON	1
#define EXYNOS4_DISPLAY_OFF	0

#define DEFAULT_DELAY_TIME	1000	/* us (millisecond) */

enum exynos4_display_type {
	TYPE_DISPLAY_EXYNOS4210,
	TYPE_DISPLAY_EXYNOS4x12,
};

struct exynos4_display_data {
	enum exynos4_display_type type;
	struct devfreq *devfreq;
	struct opp *curr_opp;
	struct device *dev;

	struct delayed_work wq_lowfreq;

	struct notifier_block nb_pm;

	unsigned int state;
	struct mutex lock;
};

/* Define frequency level */
enum display_clk_level_idx {
	LV_0 = 0,
	LV_1,
	_LV_END
};

/* Define opp table which include various frequency level */
struct display_opp_table {
	unsigned int idx;
	unsigned long clk;
	unsigned long volt;
};

static struct display_opp_table exynos4_display_clk_table[] = {
	{LV_0, 40, 0 },
	{LV_1, 60, 0 },
	{0, 0, 0 },
};

/*
 * The exynos-display driver send newly frequency to display client
 * if it receive event from sender device.
 *	List of display client device
 *	- FIMD and so on
 */
static BLOCKING_NOTIFIER_HEAD(exynos4_display_notifier_client_list);

int exynos4_display_register_client(struct notifier_block *nb)
{
	return blocking_notifier_chain_register(
			&exynos4_display_notifier_client_list, nb);
}
EXPORT_SYMBOL(exynos4_display_register_client);

int exynos4_display_unregister_client(struct notifier_block *nb)
{
	return blocking_notifier_chain_unregister(
			&exynos4_display_notifier_client_list, nb);
}
EXPORT_SYMBOL(exynos4_display_unregister_client);

static int exynos4_display_send_event_to_display(unsigned long val, void *v)
{
	return blocking_notifier_call_chain(
			&exynos4_display_notifier_client_list, val, v);
}

/*
 * Register exynos-display as client to pm notifer
 * - This callback gets called when something important happens in pm state.
 */
static int exynos4_display_pm_notifier_callback(struct notifier_block *this,
				 unsigned long event, void *_data)
{
	struct exynos4_display_data *data = container_of(this,
			struct exynos4_display_data, nb_pm);

	if (data->state == EXYNOS4_DISPLAY_OFF)
		return NOTIFY_OK;

	switch (event) {
	case PM_SUSPEND_PREPARE:
		mutex_lock(&data->lock);
		data->state = EXYNOS4_DISPLAY_OFF;
		mutex_unlock(&data->lock);

		if (delayed_work_pending(&data->wq_lowfreq))
			cancel_delayed_work(&data->wq_lowfreq);

		return NOTIFY_OK;
	case PM_POST_RESTORE:
	case PM_POST_SUSPEND:
		mutex_lock(&data->lock);
		data->state = EXYNOS4_DISPLAY_ON;
		mutex_unlock(&data->lock);

		return NOTIFY_OK;
	}

	return NOTIFY_DONE;
}

/*
 * Enable/disable exynos-display operation
 */
static void exynos4_display_disable(struct exynos4_display_data *data)
{
	struct opp *opp;
	unsigned long freq = EXYNOS4_DISPLAY_LV_DEFAULT;

	/* Cancel workqueue which set low frequency of display client
	 * if it is pending state before executing workqueue. */
	if (delayed_work_pending(&data->wq_lowfreq))
		cancel_delayed_work(&data->wq_lowfreq);

	/* Set high frequency(default) of display client */
	exynos4_display_send_event_to_display(freq, NULL);

	mutex_lock(&data->lock);
	data->state = EXYNOS4_DISPLAY_OFF;
	mutex_unlock(&data->lock);

	/* Find opp object with high frequency */
	opp = opp_find_freq_floor(data->dev, &freq);
	if (IS_ERR(opp)) {
		dev_err(data->dev,
			"invalid initial frequency %lu kHz.\n", freq);
	} else
		data->curr_opp = opp;
}

static void exynos4_display_enable(struct exynos4_display_data *data)
{
	data->state = EXYNOS4_DISPLAY_ON;
}

/*
 * Timer to set display with low frequency state after 1 second
 */
static void exynos4_display_set_lowfreq(struct work_struct *work)
{
	exynos4_display_send_event_to_display(EXYNOS4_DISPLAY_LV_LF, NULL);
}

/*
 * Send event to display client for changing frequency when DVFREQ framework
 * update state of device
 */
static int exynos4_display_profile_target(struct device *dev,
					unsigned long *_freq, u32 options)
{
	/* Inform display client of new frequency */
	struct exynos4_display_data *data = dev_get_drvdata(dev);
	struct opp *opp = devfreq_recommended_opp(dev, _freq, options &
						  DEVFREQ_OPTION_FREQ_GLB);
	unsigned long old_freq = opp_get_freq(data->curr_opp);
	unsigned long new_freq = opp_get_freq(opp);

	/* TODO: No longer use fb notifier to identify LCD on/off state and
	   have yet alternative feature of it. So, exynos4-display change
	   refresh rate of display clinet irrespective of LCD state until
	   proper feature will be implemented. */
	if (old_freq == new_freq)
		return 0;

	opp = opp_find_freq_floor(dev, &new_freq);
	data->curr_opp = opp;

	switch (new_freq) {
	case EXYNOS4_DISPLAY_LV_HF:
		if (delayed_work_pending(&data->wq_lowfreq))
			cancel_delayed_work(&data->wq_lowfreq);

		exynos4_display_send_event_to_display(
				EXYNOS4_DISPLAY_LV_HF, NULL);
		break;
	case EXYNOS4_DISPLAY_LV_LF:
		schedule_delayed_work(&data->wq_lowfreq,
				msecs_to_jiffies(DEFAULT_DELAY_TIME));
		break;
	}

	printk(KERN_DEBUG "exynos4-display: request %ldHz from \'%s\'\n",
			new_freq, dev_name(dev));

	return 0;
}

static void exynos4_display_profile_exit(struct device *dev)
{
	/* TODO */
}

static struct devfreq_dev_profile exynos4_display_profile = {
	.initial_freq	= EXYNOS4_DISPLAY_LV_DEFAULT,
	.target		= exynos4_display_profile_target,
	.exit		= exynos4_display_profile_exit,
};

static int exynos4_display_probe(struct platform_device *pdev)
{
	struct exynos4_display_data *data;
	struct device *dev = &pdev->dev;
	struct opp *opp;
	int ret = 0;
	int i;
	struct devfreq_pm_qos_table *qos_list;

	data = kzalloc(sizeof(struct exynos4_display_data), GFP_KERNEL);
	if (!data) {
		dev_err(dev, "cannot allocate memory.\n");
		return -ENOMEM;
	}
	data->dev = dev;
	data->state = EXYNOS4_DISPLAY_ON;
	mutex_init(&data->lock);

	/* Register OPP entries */
	for (i = 0 ; i < _LV_END ; i++) {
		ret = opp_add(dev, exynos4_display_clk_table[i].clk,
			      exynos4_display_clk_table[i].volt);
		if (ret) {
			dev_err(dev, "cannot add opp entries.\n");
			goto err_alloc_mem;
		}
	}

	/* Find opp object with init frequency */
	opp = opp_find_freq_floor(dev, &exynos4_display_profile.initial_freq);
	if (IS_ERR(opp)) {
		dev_err(dev, "invalid initial frequency %lu kHz.\n",
		       exynos4_display_profile.initial_freq);
		ret = PTR_ERR(opp);
		goto err_alloc_mem;
	}
	data->curr_opp = opp;

	/* Initialize QoS */
	qos_list = kzalloc(sizeof(struct devfreq_pm_qos_table) * _LV_END,
			GFP_KERNEL);
	for (i = 0 ; i < _LV_END ; i++) {
		qos_list[i].freq = exynos4_display_clk_table[i].clk;
		qos_list[i].qos_value = exynos4_display_clk_table[i].clk;
	}
	exynos4_display_profile.qos_type = PM_QOS_DISPLAY_FREQUENCY;
	exynos4_display_profile.qos_use_max = true;
	exynos4_display_profile.qos_list = qos_list;

	/* Register exynos4_display to DEVFREQ framework */
	data->devfreq = devfreq_add_device(dev, &exynos4_display_profile,
			&devfreq_powersave, NULL);
	if (IS_ERR(data->devfreq)) {
		ret = PTR_ERR(data->devfreq);
		dev_err(dev,
			"failed to add exynos4 lcd to DEVFREQ : %d\n", ret);
		goto err_alloc_mem;
	}
	devfreq_register_opp_notifier(dev, data->devfreq);

	/* Register exynos4_display as client to pm notifier */
	memset(&data->nb_pm, 0, sizeof(data->nb_pm));
	data->nb_pm.notifier_call = exynos4_display_pm_notifier_callback;
	ret = register_pm_notifier(&data->nb_pm);
	if (ret < 0) {
		dev_err(dev, "failed to get pm notifier: %d\n", ret);
		goto err_add_devfreq;
	}

	INIT_DELAYED_WORK(&data->wq_lowfreq, exynos4_display_set_lowfreq);

	platform_set_drvdata(pdev, data);

	return 0;

err_add_devfreq:
	devfreq_remove_device(data->devfreq);
err_alloc_mem:
	kfree(data);

	return ret;
}

static int __devexit exynos4_display_remove(struct platform_device *pdev)
{
	struct exynos4_display_data *data = pdev->dev.platform_data;

	unregister_pm_notifier(&data->nb_pm);
	exynos4_display_disable(data);

	devfreq_remove_device(data->devfreq);

	kfree(data);

	return 0;
}

#ifdef CONFIG_PM
static int exynos4_display_suspend(struct device *dev)
{
	/* TODO */
	return 0;
}

static int exynos4_display_resume(struct device *dev)
{
	/* TODO */
	return 0;
}

static const struct dev_pm_ops exynos4_display_dev_pm_ops = {
	.suspend	= exynos4_display_suspend,
	.resume		= exynos4_display_resume,
};

#define exynos4_display_DEV_PM_OPS	(&exynos4_display_dev_pm_ops)
#else
#define exynos4_display_DEV_PM_OPS	NULL
#endif	/* CONFIG_PM */

static struct platform_device_id exynos4_display_ids[] = {
	{ "exynos4210-display", TYPE_DISPLAY_EXYNOS4210 },
	{ "exynos4212-display", TYPE_DISPLAY_EXYNOS4x12 },
	{ "exynos4412-display", TYPE_DISPLAY_EXYNOS4x12 },
	{ },
};

static struct platform_driver exynos4_display_driver = {
	.probe = exynos4_display_probe,
	.remove	= __devexit_p(exynos4_display_remove),
	.id_table = exynos4_display_ids,
	.driver = {
		.name	= "exynos4-display",
		.owner	= THIS_MODULE,
		.pm	= exynos4_display_DEV_PM_OPS,
	},
};

static int __init exynos4_display_init(void)
{
	return platform_driver_register(&exynos4_display_driver);
}
late_initcall(exynos4_display_init);

static void __exit exynos4_display_exit(void)
{
	platform_driver_unregister(&exynos4_display_driver);
}
module_exit(exynos4_display_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("EXYNOS4 display driver with devfreq framework");
MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
MODULE_AUTHOR("Myungjoo Ham <myungjoo.ham@samsung.com>");
MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
MODULE_ALIAS("exynos-display");