Shawn Guo | 12bb3440 | 2012-12-04 22:55:14 +0800 | [diff] [blame] | 1 | /* |
| 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> |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 12 | #include <asm/proc-fns.h> |
Shawn Guo | 12bb3440 | 2012-12-04 22:55:14 +0800 | [diff] [blame] | 13 | |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 14 | #include "common.h" |
Shawn Guo | 12bb3440 | 2012-12-04 22:55:14 +0800 | [diff] [blame] | 15 | #include "cpuidle.h" |
| 16 | |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 17 | static atomic_t master = ATOMIC_INIT(0); |
| 18 | static DEFINE_SPINLOCK(master_lock); |
| 19 | |
| 20 | static int imx6q_enter_wait(struct cpuidle_device *dev, |
| 21 | struct cpuidle_driver *drv, int index) |
| 22 | { |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 23 | 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; |
| 30 | imx6q_set_lpm(WAIT_UNCLOCKED); |
| 31 | cpu_do_idle(); |
| 32 | imx6q_set_lpm(WAIT_CLOCKED); |
| 33 | spin_unlock(&master_lock); |
| 34 | goto done; |
| 35 | } |
| 36 | |
| 37 | idle: |
| 38 | cpu_do_idle(); |
| 39 | done: |
| 40 | atomic_dec(&master); |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 41 | |
| 42 | return index; |
| 43 | } |
| 44 | |
Shawn Guo | 12bb3440 | 2012-12-04 22:55:14 +0800 | [diff] [blame] | 45 | static struct cpuidle_driver imx6q_cpuidle_driver = { |
| 46 | .name = "imx6q_cpuidle", |
| 47 | .owner = THIS_MODULE, |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 48 | .states = { |
| 49 | /* WFI */ |
| 50 | ARM_CPUIDLE_WFI_STATE, |
| 51 | /* WAIT */ |
| 52 | { |
| 53 | .exit_latency = 50, |
| 54 | .target_residency = 75, |
Daniel Lezcano | 8de46ef | 2013-03-21 12:21:33 +0000 | [diff] [blame] | 55 | .flags = CPUIDLE_FLAG_TIME_VALID | |
| 56 | CPUIDLE_FLAG_TIMER_STOP, |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 57 | .enter = imx6q_enter_wait, |
| 58 | .name = "WAIT", |
| 59 | .desc = "Clock off", |
| 60 | }, |
| 61 | }, |
| 62 | .state_count = 2, |
| 63 | .safe_state_index = 0, |
Shawn Guo | 12bb3440 | 2012-12-04 22:55:14 +0800 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | int __init imx6q_cpuidle_init(void) |
| 67 | { |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 68 | /* Need to enable SCU standby for entering WAIT modes */ |
| 69 | imx_scu_standby_enable(); |
| 70 | |
| 71 | /* Set chicken bit to get a reliable WAIT mode support */ |
| 72 | imx6q_set_chicken_bit(); |
| 73 | |
Daniel Lezcano | 54a4644 | 2013-04-23 08:54:45 +0000 | [diff] [blame] | 74 | return cpuidle_register(&imx6q_cpuidle_driver, NULL); |
Shawn Guo | 12bb3440 | 2012-12-04 22:55:14 +0800 | [diff] [blame] | 75 | } |