Commit ebf2ef8f613433aaffac53aef2f6703445821fc6

Authored by Andy Shevchenko
Committed by Wolfram Sang
1 parent 30be774b38

i2c: designware-baytrail: baytrail_i2c_acquire() might sleep

This patch marks baytrail_i2c_acquire() that it might sleep. Also it chages
while-loop to do-while and, though it is matter of taste, gives a chance to
check one more time before report a timeout.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Acked-by: David E. Box <david.e.box@linux.intel.com>
Signed-off-by: Wolfram Sang <wsa@the-dreams.de>

Showing 1 changed file with 4 additions and 2 deletions Inline Diff

drivers/i2c/busses/i2c-designware-baytrail.c
1 /* 1 /*
2 * Intel BayTrail PMIC I2C bus semaphore implementaion 2 * Intel BayTrail PMIC I2C bus semaphore implementaion
3 * Copyright (c) 2014, Intel Corporation. 3 * Copyright (c) 2014, Intel Corporation.
4 * 4 *
5 * This program is free software; you can redistribute it and/or modify it 5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License, 6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation. 7 * version 2, as published by the Free Software Foundation.
8 * 8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT 9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details. 12 * more details.
13 */ 13 */
14 #include <linux/module.h> 14 #include <linux/module.h>
15 #include <linux/delay.h> 15 #include <linux/delay.h>
16 #include <linux/device.h> 16 #include <linux/device.h>
17 #include <linux/acpi.h> 17 #include <linux/acpi.h>
18 #include <linux/i2c.h> 18 #include <linux/i2c.h>
19 #include <linux/interrupt.h> 19 #include <linux/interrupt.h>
20 20
21 #include <asm/iosf_mbi.h> 21 #include <asm/iosf_mbi.h>
22 22
23 #include "i2c-designware-core.h" 23 #include "i2c-designware-core.h"
24 24
25 #define SEMAPHORE_TIMEOUT 100 25 #define SEMAPHORE_TIMEOUT 100
26 #define PUNIT_SEMAPHORE 0x7 26 #define PUNIT_SEMAPHORE 0x7
27 #define PUNIT_SEMAPHORE_BIT BIT(0) 27 #define PUNIT_SEMAPHORE_BIT BIT(0)
28 #define PUNIT_SEMAPHORE_ACQUIRE BIT(1) 28 #define PUNIT_SEMAPHORE_ACQUIRE BIT(1)
29 29
30 static unsigned long acquired; 30 static unsigned long acquired;
31 31
32 static int get_sem(struct device *dev, u32 *sem) 32 static int get_sem(struct device *dev, u32 *sem)
33 { 33 {
34 u32 data; 34 u32 data;
35 int ret; 35 int ret;
36 36
37 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ, PUNIT_SEMAPHORE, 37 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ, PUNIT_SEMAPHORE,
38 &data); 38 &data);
39 if (ret) { 39 if (ret) {
40 dev_err(dev, "iosf failed to read punit semaphore\n"); 40 dev_err(dev, "iosf failed to read punit semaphore\n");
41 return ret; 41 return ret;
42 } 42 }
43 43
44 *sem = data & PUNIT_SEMAPHORE_BIT; 44 *sem = data & PUNIT_SEMAPHORE_BIT;
45 45
46 return 0; 46 return 0;
47 } 47 }
48 48
49 static void reset_semaphore(struct device *dev) 49 static void reset_semaphore(struct device *dev)
50 { 50 {
51 u32 data; 51 u32 data;
52 52
53 if (iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ, 53 if (iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ,
54 PUNIT_SEMAPHORE, &data)) { 54 PUNIT_SEMAPHORE, &data)) {
55 dev_err(dev, "iosf failed to reset punit semaphore during read\n"); 55 dev_err(dev, "iosf failed to reset punit semaphore during read\n");
56 return; 56 return;
57 } 57 }
58 58
59 data &= ~PUNIT_SEMAPHORE_BIT; 59 data &= ~PUNIT_SEMAPHORE_BIT;
60 if (iosf_mbi_write(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_WRITE, 60 if (iosf_mbi_write(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_WRITE,
61 PUNIT_SEMAPHORE, data)) 61 PUNIT_SEMAPHORE, data))
62 dev_err(dev, "iosf failed to reset punit semaphore during write\n"); 62 dev_err(dev, "iosf failed to reset punit semaphore during write\n");
63 } 63 }
64 64
65 static int baytrail_i2c_acquire(struct dw_i2c_dev *dev) 65 static int baytrail_i2c_acquire(struct dw_i2c_dev *dev)
66 { 66 {
67 u32 sem; 67 u32 sem;
68 int ret; 68 int ret;
69 unsigned long start, end; 69 unsigned long start, end;
70 70
71 might_sleep();
72
71 if (!dev || !dev->dev) 73 if (!dev || !dev->dev)
72 return -ENODEV; 74 return -ENODEV;
73 75
74 if (!dev->release_lock) 76 if (!dev->release_lock)
75 return 0; 77 return 0;
76 78
77 /* host driver writes to side band semaphore register */ 79 /* host driver writes to side band semaphore register */
78 ret = iosf_mbi_write(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_WRITE, 80 ret = iosf_mbi_write(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_WRITE,
79 PUNIT_SEMAPHORE, PUNIT_SEMAPHORE_ACQUIRE); 81 PUNIT_SEMAPHORE, PUNIT_SEMAPHORE_ACQUIRE);
80 if (ret) { 82 if (ret) {
81 dev_err(dev->dev, "iosf punit semaphore request failed\n"); 83 dev_err(dev->dev, "iosf punit semaphore request failed\n");
82 return ret; 84 return ret;
83 } 85 }
84 86
85 /* host driver waits for bit 0 to be set in semaphore register */ 87 /* host driver waits for bit 0 to be set in semaphore register */
86 start = jiffies; 88 start = jiffies;
87 end = start + msecs_to_jiffies(SEMAPHORE_TIMEOUT); 89 end = start + msecs_to_jiffies(SEMAPHORE_TIMEOUT);
88 while (!time_after(jiffies, end)) { 90 do {
89 ret = get_sem(dev->dev, &sem); 91 ret = get_sem(dev->dev, &sem);
90 if (!ret && sem) { 92 if (!ret && sem) {
91 acquired = jiffies; 93 acquired = jiffies;
92 dev_dbg(dev->dev, "punit semaphore acquired after %ums\n", 94 dev_dbg(dev->dev, "punit semaphore acquired after %ums\n",
93 jiffies_to_msecs(jiffies - start)); 95 jiffies_to_msecs(jiffies - start));
94 return 0; 96 return 0;
95 } 97 }
96 98
97 usleep_range(1000, 2000); 99 usleep_range(1000, 2000);
98 } 100 } while (time_before(jiffies, end));
99 101
100 dev_err(dev->dev, "punit semaphore timed out, resetting\n"); 102 dev_err(dev->dev, "punit semaphore timed out, resetting\n");
101 reset_semaphore(dev->dev); 103 reset_semaphore(dev->dev);
102 104
103 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ, 105 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ,
104 PUNIT_SEMAPHORE, &sem); 106 PUNIT_SEMAPHORE, &sem);
105 if (ret) 107 if (ret)
106 dev_err(dev->dev, "iosf failed to read punit semaphore\n"); 108 dev_err(dev->dev, "iosf failed to read punit semaphore\n");
107 else 109 else
108 dev_err(dev->dev, "PUNIT SEM: %d\n", sem); 110 dev_err(dev->dev, "PUNIT SEM: %d\n", sem);
109 111
110 WARN_ON(1); 112 WARN_ON(1);
111 113
112 return -ETIMEDOUT; 114 return -ETIMEDOUT;
113 } 115 }
114 116
115 static void baytrail_i2c_release(struct dw_i2c_dev *dev) 117 static void baytrail_i2c_release(struct dw_i2c_dev *dev)
116 { 118 {
117 if (!dev || !dev->dev) 119 if (!dev || !dev->dev)
118 return; 120 return;
119 121
120 if (!dev->acquire_lock) 122 if (!dev->acquire_lock)
121 return; 123 return;
122 124
123 reset_semaphore(dev->dev); 125 reset_semaphore(dev->dev);
124 dev_dbg(dev->dev, "punit semaphore held for %ums\n", 126 dev_dbg(dev->dev, "punit semaphore held for %ums\n",
125 jiffies_to_msecs(jiffies - acquired)); 127 jiffies_to_msecs(jiffies - acquired));
126 } 128 }
127 129
128 int i2c_dw_eval_lock_support(struct dw_i2c_dev *dev) 130 int i2c_dw_eval_lock_support(struct dw_i2c_dev *dev)
129 { 131 {
130 acpi_status status; 132 acpi_status status;
131 unsigned long long shared_host = 0; 133 unsigned long long shared_host = 0;
132 acpi_handle handle; 134 acpi_handle handle;
133 135
134 if (!dev || !dev->dev) 136 if (!dev || !dev->dev)
135 return 0; 137 return 0;
136 138
137 handle = ACPI_HANDLE(dev->dev); 139 handle = ACPI_HANDLE(dev->dev);
138 if (!handle) 140 if (!handle)
139 return 0; 141 return 0;
140 142
141 status = acpi_evaluate_integer(handle, "_SEM", NULL, &shared_host); 143 status = acpi_evaluate_integer(handle, "_SEM", NULL, &shared_host);
142 if (ACPI_FAILURE(status)) 144 if (ACPI_FAILURE(status))
143 return 0; 145 return 0;
144 146
145 if (shared_host) { 147 if (shared_host) {
146 dev_info(dev->dev, "I2C bus managed by PUNIT\n"); 148 dev_info(dev->dev, "I2C bus managed by PUNIT\n");
147 dev->acquire_lock = baytrail_i2c_acquire; 149 dev->acquire_lock = baytrail_i2c_acquire;
148 dev->release_lock = baytrail_i2c_release; 150 dev->release_lock = baytrail_i2c_release;
149 dev->pm_runtime_disabled = true; 151 dev->pm_runtime_disabled = true;
150 } 152 }
151 153
152 if (!iosf_mbi_available()) 154 if (!iosf_mbi_available())
153 return -EPROBE_DEFER; 155 return -EPROBE_DEFER;
154 156
155 return 0; 157 return 0;
156 } 158 }
157 159
158 MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>"); 160 MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>");
159 MODULE_DESCRIPTION("Baytrail I2C Semaphore driver"); 161 MODULE_DESCRIPTION("Baytrail I2C Semaphore driver");
160 MODULE_LICENSE("GPL v2"); 162 MODULE_LICENSE("GPL v2");
161 163