2 * Intel BayTrail PMIC I2C bus semaphore implementaion
3 * Copyright (c) 2014, Intel Corporation.
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,
7 * version 2, as published by the Free Software Foundation.
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
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 #include <linux/module.h>
15 #include <linux/delay.h>
16 #include <linux/device.h>
17 #include <linux/acpi.h>
18 #include <linux/i2c.h>
19 #include <linux/interrupt.h>
20 #include <asm/iosf_mbi.h>
21 #include "i2c-designware-core.h"
23 #define SEMAPHORE_TIMEOUT 100
24 #define PUNIT_SEMAPHORE 0x7
26 static unsigned long acquired;
28 static int get_sem(struct device *dev, u32 *sem)
33 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ, PUNIT_SEMAPHORE,
36 dev_err(dev, "iosf failed to read punit semaphore\n");
45 static void reset_semaphore(struct device *dev)
49 if (iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ,
50 PUNIT_SEMAPHORE, &data)) {
51 dev_err(dev, "iosf failed to reset punit semaphore during read\n");
55 data = data & 0xfffffffe;
56 if (iosf_mbi_write(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_WRITE,
57 PUNIT_SEMAPHORE, data))
58 dev_err(dev, "iosf failed to reset punit semaphore during write\n");
61 int baytrail_i2c_acquire(struct dw_i2c_dev *dev)
65 unsigned long start, end;
67 if (!dev || !dev->dev)
70 if (!dev->acquire_lock)
73 /* host driver writes 0x2 to side band semaphore register */
74 ret = iosf_mbi_write(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_WRITE,
75 PUNIT_SEMAPHORE, 0x2);
77 dev_err(dev->dev, "iosf punit semaphore request failed\n");
81 /* host driver waits for bit 0 to be set in semaphore register */
83 end = start + msecs_to_jiffies(SEMAPHORE_TIMEOUT);
84 while (!time_after(jiffies, end)) {
85 ret = get_sem(dev->dev, &sem);
88 dev_dbg(dev->dev, "punit semaphore acquired after %ums\n",
89 jiffies_to_msecs(jiffies - start));
93 usleep_range(1000, 2000);
96 dev_err(dev->dev, "punit semaphore timed out, resetting\n");
97 reset_semaphore(dev->dev);
99 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ,
100 PUNIT_SEMAPHORE, &sem);
102 dev_err(dev->dev, "iosf failed to read punit semaphore\n");
104 dev_err(dev->dev, "PUNIT SEM: %d\n", sem);
110 EXPORT_SYMBOL(baytrail_i2c_acquire);
112 void baytrail_i2c_release(struct dw_i2c_dev *dev)
114 if (!dev || !dev->dev)
117 if (!dev->acquire_lock)
120 reset_semaphore(dev->dev);
121 dev_dbg(dev->dev, "punit semaphore held for %ums\n",
122 jiffies_to_msecs(jiffies - acquired));
124 EXPORT_SYMBOL(baytrail_i2c_release);
126 int i2c_dw_eval_lock_support(struct dw_i2c_dev *dev)
129 unsigned long long shared_host = 0;
132 if (!dev || !dev->dev)
135 handle = ACPI_HANDLE(dev->dev);
139 status = acpi_evaluate_integer(handle, "_SEM", NULL, &shared_host);
141 if (ACPI_FAILURE(status))
145 dev_info(dev->dev, "I2C bus managed by PUNIT\n");
146 dev->acquire_lock = baytrail_i2c_acquire;
147 dev->release_lock = baytrail_i2c_release;
148 dev->pm_runtime_disabled = true;
151 if (!iosf_mbi_available())
152 return -EPROBE_DEFER;
156 EXPORT_SYMBOL(i2c_dw_eval_lock_support);
158 MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>");
159 MODULE_DESCRIPTION("Baytrail I2C Semaphore driver");
160 MODULE_LICENSE("GPL v2");