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/delay.h>
15 #include <linux/device.h>
16 #include <linux/acpi.h>
17 #include <linux/i2c.h>
18 #include <linux/interrupt.h>
20 #include <asm/iosf_mbi.h>
22 #include "i2c-designware-core.h"
24 #define SEMAPHORE_TIMEOUT 100
25 #define PUNIT_SEMAPHORE 0x7
26 #define PUNIT_SEMAPHORE_BIT BIT(0)
27 #define PUNIT_SEMAPHORE_ACQUIRE BIT(1)
29 static unsigned long acquired;
31 static int get_sem(struct device *dev, u32 *sem)
36 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, PUNIT_SEMAPHORE, &data);
38 dev_err(dev, "iosf failed to read punit semaphore\n");
42 *sem = data & PUNIT_SEMAPHORE_BIT;
47 static void reset_semaphore(struct device *dev)
51 if (iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, PUNIT_SEMAPHORE, &data)) {
52 dev_err(dev, "iosf failed to reset punit semaphore during read\n");
56 data &= ~PUNIT_SEMAPHORE_BIT;
57 if (iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, PUNIT_SEMAPHORE, data))
58 dev_err(dev, "iosf failed to reset punit semaphore during write\n");
61 static int baytrail_i2c_acquire(struct dw_i2c_dev *dev)
63 u32 sem = PUNIT_SEMAPHORE_ACQUIRE;
65 unsigned long start, end;
69 if (!dev || !dev->dev)
72 if (!dev->release_lock)
75 /* host driver writes to side band semaphore register */
76 ret = iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, PUNIT_SEMAPHORE, sem);
78 dev_err(dev->dev, "iosf punit semaphore request failed\n");
82 /* host driver waits for bit 0 to be set in semaphore register */
84 end = start + msecs_to_jiffies(SEMAPHORE_TIMEOUT);
86 ret = get_sem(dev->dev, &sem);
89 dev_dbg(dev->dev, "punit semaphore acquired after %ums\n",
90 jiffies_to_msecs(jiffies - start));
94 usleep_range(1000, 2000);
95 } while (time_before(jiffies, end));
97 dev_err(dev->dev, "punit semaphore timed out, resetting\n");
98 reset_semaphore(dev->dev);
100 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, 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);
111 static void baytrail_i2c_release(struct dw_i2c_dev *dev)
113 if (!dev || !dev->dev)
116 if (!dev->acquire_lock)
119 reset_semaphore(dev->dev);
120 dev_dbg(dev->dev, "punit semaphore held for %ums\n",
121 jiffies_to_msecs(jiffies - acquired));
124 int i2c_dw_eval_lock_support(struct dw_i2c_dev *dev)
127 unsigned long long shared_host = 0;
130 if (!dev || !dev->dev)
133 handle = ACPI_HANDLE(dev->dev);
137 status = acpi_evaluate_integer(handle, "_SEM", NULL, &shared_host);
138 if (ACPI_FAILURE(status))
142 dev_info(dev->dev, "I2C bus managed by PUNIT\n");
143 dev->acquire_lock = baytrail_i2c_acquire;
144 dev->release_lock = baytrail_i2c_release;
145 dev->pm_runtime_disabled = true;
148 if (!iosf_mbi_available())
149 return -EPROBE_DEFER;