2 * drivers/misc/throughput.c
4 * Copyright (C) 2012, NVIDIA CORPORATION. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include <linux/kthread.h>
21 #include <linux/ktime.h>
22 #include <linux/notifier.h>
23 #include <linux/miscdevice.h>
25 #include <linux/init.h>
26 #include <linux/spinlock.h>
27 #include <linux/throughput_ioctl.h>
28 #include <linux/module.h>
31 #define DEFAULT_SYNC_RATE 60000 /* 60 Hz */
33 static unsigned short target_frame_time;
34 static unsigned short last_frame_time;
35 static ktime_t last_flip;
36 static unsigned int multiple_app_disable;
38 static spinlock_t lock;
40 static int throughput_flip_notifier(struct notifier_block *nb,
44 /* only register flips when a single display is active */
45 if (val != 1 || multiple_app_disable)
53 if (last_flip.tv64 != 0) {
54 timediff = (long) ktime_us_delta(now, last_flip);
55 if (timediff > (long) USHRT_MAX)
56 last_frame_time = USHRT_MAX;
58 last_frame_time = (unsigned short) timediff;
61 ((int) target_frame_time * 100)/last_frame_time;
63 /* notify throughput hint clients here */
71 static struct notifier_block throughput_flip_nb = {
72 .notifier_call = throughput_flip_notifier,
76 static int throughput_active_app_count;
78 static void reset_target_frame_time(void)
81 sync_rate = tegra_dc_get_panel_sync_rate();
84 sync_rate = DEFAULT_SYNC_RATE;
87 target_frame_time = (unsigned short) (1000000000 / sync_rate);
89 pr_debug("%s: panel sync rate %d, target frame time %u\n",
90 __func__, sync_rate, target_frame_time);
93 static int notifier_initialized;
95 static int throughput_open(struct inode *inode, struct file *file)
97 if (!notifier_initialized) {
98 tegra_dc_register_flip_notifier(&throughput_flip_nb);
99 notifier_initialized = 1;
104 throughput_active_app_count++;
105 if (throughput_active_app_count > 1)
106 multiple_app_disable = 1;
110 pr_debug("throughput_open node %p file %p\n", inode, file);
115 static int throughput_release(struct inode *inode, struct file *file)
118 throughput_active_app_count--;
121 if (throughput_active_app_count == 0) {
122 reset_target_frame_time();
123 multiple_app_disable = 0;
124 tegra_dc_unregister_flip_notifier(&throughput_flip_nb);
125 notifier_initialized = 0;
129 pr_debug("throughput_release node %p file %p\n", inode, file);
134 static int throughput_set_target_fps(unsigned long arg)
138 pr_debug("%s: target fps %lu requested\n", __func__, arg);
140 disable = multiple_app_disable;
143 pr_debug("%s: %d active apps, disabling fps usage\n",
144 __func__, throughput_active_app_count);
149 reset_target_frame_time();
151 unsigned long frame_time = (1000000 / arg);
153 if (frame_time > USHRT_MAX)
154 frame_time = USHRT_MAX;
156 target_frame_time = (unsigned short) frame_time;
163 throughput_ioctl(struct file *file,
169 if ((_IOC_TYPE(cmd) != TEGRA_THROUGHPUT_MAGIC) ||
170 (_IOC_NR(cmd) == 0) ||
171 (_IOC_NR(cmd) > TEGRA_THROUGHPUT_IOCTL_MAXNR))
175 case TEGRA_THROUGHPUT_IOCTL_TARGET_FPS:
176 pr_debug("%s: TEGRA_THROUGHPUT_IOCTL_TARGET_FPS %lu\n",
178 err = throughput_set_target_fps(arg);
188 static const struct file_operations throughput_user_fops = {
189 .owner = THIS_MODULE,
190 .open = throughput_open,
191 .release = throughput_release,
192 .unlocked_ioctl = throughput_ioctl,
195 #define TEGRA_THROUGHPUT_MINOR 1
197 static struct miscdevice throughput_miscdev = {
198 .minor = TEGRA_THROUGHPUT_MINOR,
199 .name = "tegra-throughput",
200 .fops = &throughput_user_fops,
204 int __init throughput_init_miscdev(void)
208 pr_debug("%s: initializing\n", __func__);
210 spin_lock_init(&lock);
212 ret = misc_register(&throughput_miscdev);
214 pr_err("can\'t reigster throughput miscdev"
215 " (minor %d err %d)\n", TEGRA_THROUGHPUT_MINOR, ret);
222 module_init(throughput_init_miscdev);
224 void __exit throughput_exit_miscdev(void)
226 pr_debug("%s: exiting\n", __func__);
228 misc_deregister(&throughput_miscdev);
231 module_exit(throughput_exit_miscdev);