blob: 353bb8774112d8068a072d0afe8f8f32a61683ae [file] [log] [blame]
Shawn Guo12bb34402012-12-04 22:55:14 +08001/*
2 * Copyright (C) 2012 Freescale Semiconductor, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/cpuidle.h>
10#include <linux/module.h>
11#include <asm/cpuidle.h>
12
Shawn Guoe5f9dec2012-12-04 22:55:15 +080013#include "common.h"
Shawn Guo12bb34402012-12-04 22:55:14 +080014#include "cpuidle.h"
Anson Huanga25d67a2014-06-20 13:44:05 +080015#include "hardware.h"
Shawn Guo12bb34402012-12-04 22:55:14 +080016
Shawn Guoe5f9dec2012-12-04 22:55:15 +080017static atomic_t master = ATOMIC_INIT(0);
18static DEFINE_SPINLOCK(master_lock);
19
20static int imx6q_enter_wait(struct cpuidle_device *dev,
21 struct cpuidle_driver *drv, int index)
22{
Shawn Guoe5f9dec2012-12-04 22:55:15 +080023 if (atomic_inc_return(&master) == num_online_cpus()) {
24 /*
25 * With this lock, we prevent other cpu to exit and enter
26 * this function again and become the master.
27 */
28 if (!spin_trylock(&master_lock))
29 goto idle;
Shawn Guo8fb76a02015-04-25 22:59:19 +080030 imx6_set_lpm(WAIT_UNCLOCKED);
Shawn Guoe5f9dec2012-12-04 22:55:15 +080031 cpu_do_idle();
Shawn Guo8fb76a02015-04-25 22:59:19 +080032 imx6_set_lpm(WAIT_CLOCKED);
Shawn Guoe5f9dec2012-12-04 22:55:15 +080033 spin_unlock(&master_lock);
34 goto done;
35 }
36
37idle:
38 cpu_do_idle();
39done:
40 atomic_dec(&master);
Shawn Guoe5f9dec2012-12-04 22:55:15 +080041
42 return index;
43}
44
Shawn Guo12bb34402012-12-04 22:55:14 +080045static struct cpuidle_driver imx6q_cpuidle_driver = {
46 .name = "imx6q_cpuidle",
47 .owner = THIS_MODULE,
Shawn Guoe5f9dec2012-12-04 22:55:15 +080048 .states = {
49 /* WFI */
50 ARM_CPUIDLE_WFI_STATE,
51 /* WAIT */
52 {
53 .exit_latency = 50,
54 .target_residency = 75,
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +010055 .flags = CPUIDLE_FLAG_TIMER_STOP,
Shawn Guoe5f9dec2012-12-04 22:55:15 +080056 .enter = imx6q_enter_wait,
57 .name = "WAIT",
58 .desc = "Clock off",
59 },
60 },
61 .state_count = 2,
62 .safe_state_index = 0,
Shawn Guo12bb34402012-12-04 22:55:14 +080063};
64
65int __init imx6q_cpuidle_init(void)
66{
Fabio Estevamfa6be652014-01-07 08:00:40 -020067 /* Set INT_MEM_CLK_LPM bit to get a reliable WAIT mode support */
Anson Huangdfea9532014-06-23 16:42:43 +080068 imx6q_set_int_mem_clk_lpm(true);
Shawn Guoe5f9dec2012-12-04 22:55:15 +080069
Daniel Lezcano54a46442013-04-23 08:54:45 +000070 return cpuidle_register(&imx6q_cpuidle_driver, NULL);
Shawn Guo12bb34402012-12-04 22:55:14 +080071}