vt: vt_ioctl: fix VT_DISALLOCATE freeing in-use virtual console
[linux/fpc-iii.git] / drivers / i2c / busses / i2c-designware-baytrail.c
bloba2a275cfc1f694896bf112b2862f1bd8f2530ece
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Intel BayTrail PMIC I2C bus semaphore implementaion
4 * Copyright (c) 2014, Intel Corporation.
5 */
6 #include <linux/delay.h>
7 #include <linux/device.h>
8 #include <linux/acpi.h>
9 #include <linux/i2c.h>
10 #include <linux/interrupt.h>
11 #include <linux/pm_qos.h>
13 #include <asm/iosf_mbi.h>
15 #include "i2c-designware-core.h"
17 #define SEMAPHORE_TIMEOUT 500
18 #define PUNIT_SEMAPHORE 0x7
19 #define PUNIT_SEMAPHORE_CHT 0x10e
20 #define PUNIT_SEMAPHORE_BIT BIT(0)
21 #define PUNIT_SEMAPHORE_ACQUIRE BIT(1)
23 static unsigned long acquired;
25 static u32 get_sem_addr(struct dw_i2c_dev *dev)
27 if (dev->flags & MODEL_CHERRYTRAIL)
28 return PUNIT_SEMAPHORE_CHT;
29 else
30 return PUNIT_SEMAPHORE;
33 static int get_sem(struct dw_i2c_dev *dev, u32 *sem)
35 u32 addr = get_sem_addr(dev);
36 u32 data;
37 int ret;
39 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, addr, &data);
40 if (ret) {
41 dev_err(dev->dev, "iosf failed to read punit semaphore\n");
42 return ret;
45 *sem = data & PUNIT_SEMAPHORE_BIT;
47 return 0;
50 static void reset_semaphore(struct dw_i2c_dev *dev)
52 if (iosf_mbi_modify(BT_MBI_UNIT_PMC, MBI_REG_READ, get_sem_addr(dev),
53 0, PUNIT_SEMAPHORE_BIT))
54 dev_err(dev->dev, "iosf failed to reset punit semaphore during write\n");
56 pm_qos_update_request(&dev->pm_qos, PM_QOS_DEFAULT_VALUE);
58 iosf_mbi_call_pmic_bus_access_notifier_chain(MBI_PMIC_BUS_ACCESS_END,
59 NULL);
60 iosf_mbi_punit_release();
63 static int baytrail_i2c_acquire(struct dw_i2c_dev *dev)
65 u32 addr;
66 u32 sem = PUNIT_SEMAPHORE_ACQUIRE;
67 int ret;
68 unsigned long start, end;
70 might_sleep();
72 if (!dev || !dev->dev)
73 return -ENODEV;
75 if (!dev->release_lock)
76 return 0;
78 iosf_mbi_punit_acquire();
79 iosf_mbi_call_pmic_bus_access_notifier_chain(MBI_PMIC_BUS_ACCESS_BEGIN,
80 NULL);
83 * Disallow the CPU to enter C6 or C7 state, entering these states
84 * requires the punit to talk to the pmic and if this happens while
85 * we're holding the semaphore, the SoC hangs.
87 pm_qos_update_request(&dev->pm_qos, 0);
89 addr = get_sem_addr(dev);
91 /* host driver writes to side band semaphore register */
92 ret = iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, addr, sem);
93 if (ret) {
94 dev_err(dev->dev, "iosf punit semaphore request failed\n");
95 goto out;
98 /* host driver waits for bit 0 to be set in semaphore register */
99 start = jiffies;
100 end = start + msecs_to_jiffies(SEMAPHORE_TIMEOUT);
101 do {
102 ret = get_sem(dev, &sem);
103 if (!ret && sem) {
104 acquired = jiffies;
105 dev_dbg(dev->dev, "punit semaphore acquired after %ums\n",
106 jiffies_to_msecs(jiffies - start));
107 return 0;
110 usleep_range(1000, 2000);
111 } while (time_before(jiffies, end));
113 dev_err(dev->dev, "punit semaphore timed out, resetting\n");
114 out:
115 reset_semaphore(dev);
117 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, addr, &sem);
118 if (ret)
119 dev_err(dev->dev, "iosf failed to read punit semaphore\n");
120 else
121 dev_err(dev->dev, "PUNIT SEM: %d\n", sem);
123 WARN_ON(1);
125 return -ETIMEDOUT;
128 static void baytrail_i2c_release(struct dw_i2c_dev *dev)
130 if (!dev || !dev->dev)
131 return;
133 if (!dev->acquire_lock)
134 return;
136 reset_semaphore(dev);
137 dev_dbg(dev->dev, "punit semaphore held for %ums\n",
138 jiffies_to_msecs(jiffies - acquired));
141 int i2c_dw_probe_lock_support(struct dw_i2c_dev *dev)
143 acpi_status status;
144 unsigned long long shared_host = 0;
145 acpi_handle handle;
147 if (!dev || !dev->dev)
148 return 0;
150 handle = ACPI_HANDLE(dev->dev);
151 if (!handle)
152 return 0;
154 status = acpi_evaluate_integer(handle, "_SEM", NULL, &shared_host);
155 if (ACPI_FAILURE(status))
156 return 0;
158 if (!shared_host)
159 return 0;
161 if (!iosf_mbi_available())
162 return -EPROBE_DEFER;
164 dev_info(dev->dev, "I2C bus managed by PUNIT\n");
165 dev->acquire_lock = baytrail_i2c_acquire;
166 dev->release_lock = baytrail_i2c_release;
167 dev->pm_disabled = true;
169 pm_qos_add_request(&dev->pm_qos, PM_QOS_CPU_DMA_LATENCY,
170 PM_QOS_DEFAULT_VALUE);
172 return 0;
175 void i2c_dw_remove_lock_support(struct dw_i2c_dev *dev)
177 if (dev->acquire_lock)
178 pm_qos_remove_request(&dev->pm_qos);