2023-04-15 18:43:02 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2008-11-13 05:27:09 +08:00
|
|
|
/*
|
2012-06-22 11:40:40 +08:00
|
|
|
* Copyright (C) 2007,2012 Texas Instruments, Inc.
|
2008-11-13 05:27:09 +08:00
|
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/interrupt.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2008-11-13 05:27:09 +08:00
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/io.h>
|
2010-04-24 01:18:03 +08:00
|
|
|
#include <linux/sched.h>
|
2012-06-22 11:40:40 +08:00
|
|
|
#include <linux/pm_runtime.h>
|
2015-09-15 01:54:33 +08:00
|
|
|
#include <linux/of.h>
|
2008-11-13 05:27:09 +08:00
|
|
|
|
2017-06-05 21:52:08 +08:00
|
|
|
#include <linux/w1.h>
|
2008-11-13 05:27:09 +08:00
|
|
|
|
|
|
|
#define MOD_NAME "OMAP_HDQ:"
|
|
|
|
|
|
|
|
#define OMAP_HDQ_REVISION 0x00
|
|
|
|
#define OMAP_HDQ_TX_DATA 0x04
|
|
|
|
#define OMAP_HDQ_RX_DATA 0x08
|
|
|
|
#define OMAP_HDQ_CTRL_STATUS 0x0c
|
2015-09-15 01:54:33 +08:00
|
|
|
#define OMAP_HDQ_CTRL_STATUS_SINGLE BIT(7)
|
|
|
|
#define OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK BIT(6)
|
|
|
|
#define OMAP_HDQ_CTRL_STATUS_CLOCKENABLE BIT(5)
|
|
|
|
#define OMAP_HDQ_CTRL_STATUS_GO BIT(4)
|
|
|
|
#define OMAP_HDQ_CTRL_STATUS_PRESENCE BIT(3)
|
|
|
|
#define OMAP_HDQ_CTRL_STATUS_INITIALIZATION BIT(2)
|
|
|
|
#define OMAP_HDQ_CTRL_STATUS_DIR BIT(1)
|
2008-11-13 05:27:09 +08:00
|
|
|
#define OMAP_HDQ_INT_STATUS 0x10
|
2015-09-15 01:54:33 +08:00
|
|
|
#define OMAP_HDQ_INT_STATUS_TXCOMPLETE BIT(2)
|
|
|
|
#define OMAP_HDQ_INT_STATUS_RXCOMPLETE BIT(1)
|
|
|
|
#define OMAP_HDQ_INT_STATUS_TIMEOUT BIT(0)
|
2008-11-13 05:27:09 +08:00
|
|
|
|
|
|
|
#define OMAP_HDQ_FLAG_CLEAR 0
|
|
|
|
#define OMAP_HDQ_FLAG_SET 1
|
|
|
|
#define OMAP_HDQ_TIMEOUT (HZ/5)
|
|
|
|
|
|
|
|
#define OMAP_HDQ_MAX_USER 4
|
|
|
|
|
|
|
|
static DECLARE_WAIT_QUEUE_HEAD(hdq_wait_queue);
|
2017-05-17 04:02:12 +08:00
|
|
|
|
2008-11-13 05:27:09 +08:00
|
|
|
static int w1_id;
|
2023-04-15 18:42:54 +08:00
|
|
|
module_param(w1_id, int, 0400);
|
2017-05-17 04:02:12 +08:00
|
|
|
MODULE_PARM_DESC(w1_id, "1-wire id for the slave detection in HDQ mode");
|
2008-11-13 05:27:09 +08:00
|
|
|
|
|
|
|
struct hdq_data {
|
|
|
|
struct device *dev;
|
|
|
|
void __iomem *hdq_base;
|
2020-05-24 01:32:56 +08:00
|
|
|
/* lock read/write/break operations */
|
2008-11-13 05:27:09 +08:00
|
|
|
struct mutex hdq_mutex;
|
2020-05-24 01:32:56 +08:00
|
|
|
/* interrupt status and a lock for it */
|
2008-11-13 05:27:09 +08:00
|
|
|
u8 hdq_irqstatus;
|
|
|
|
spinlock_t hdq_spinlock;
|
2015-09-15 01:54:33 +08:00
|
|
|
/* mode: 0-HDQ 1-W1 */
|
|
|
|
int mode;
|
|
|
|
|
2008-11-13 05:27:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* HDQ register I/O routines */
|
|
|
|
static inline u8 hdq_reg_in(struct hdq_data *hdq_data, u32 offset)
|
|
|
|
{
|
2012-06-22 11:40:37 +08:00
|
|
|
return __raw_readl(hdq_data->hdq_base + offset);
|
2008-11-13 05:27:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void hdq_reg_out(struct hdq_data *hdq_data, u32 offset, u8 val)
|
|
|
|
{
|
2012-06-22 11:40:37 +08:00
|
|
|
__raw_writel(val, hdq_data->hdq_base + offset);
|
2008-11-13 05:27:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline u8 hdq_reg_merge(struct hdq_data *hdq_data, u32 offset,
|
|
|
|
u8 val, u8 mask)
|
|
|
|
{
|
2012-06-22 11:40:37 +08:00
|
|
|
u8 new_val = (__raw_readl(hdq_data->hdq_base + offset) & ~mask)
|
2008-11-13 05:27:09 +08:00
|
|
|
| (val & mask);
|
2012-06-22 11:40:37 +08:00
|
|
|
__raw_writel(new_val, hdq_data->hdq_base + offset);
|
2008-11-13 05:27:09 +08:00
|
|
|
|
|
|
|
return new_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for one or more bits in flag change.
|
|
|
|
* HDQ_FLAG_SET: wait until any bit in the flag is set.
|
|
|
|
* HDQ_FLAG_CLEAR: wait until all bits in the flag are cleared.
|
|
|
|
* return 0 on success and -ETIMEDOUT in the case of timeout.
|
|
|
|
*/
|
|
|
|
static int hdq_wait_for_flag(struct hdq_data *hdq_data, u32 offset,
|
|
|
|
u8 flag, u8 flag_set, u8 *status)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
unsigned long timeout = jiffies + OMAP_HDQ_TIMEOUT;
|
|
|
|
|
|
|
|
if (flag_set == OMAP_HDQ_FLAG_CLEAR) {
|
|
|
|
/* wait for the flag clear */
|
|
|
|
while (((*status = hdq_reg_in(hdq_data, offset)) & flag)
|
|
|
|
&& time_before(jiffies, timeout)) {
|
|
|
|
schedule_timeout_uninterruptible(1);
|
|
|
|
}
|
|
|
|
if (*status & flag)
|
|
|
|
ret = -ETIMEDOUT;
|
|
|
|
} else if (flag_set == OMAP_HDQ_FLAG_SET) {
|
|
|
|
/* wait for the flag set */
|
|
|
|
while (!((*status = hdq_reg_in(hdq_data, offset)) & flag)
|
|
|
|
&& time_before(jiffies, timeout)) {
|
|
|
|
schedule_timeout_uninterruptible(1);
|
|
|
|
}
|
|
|
|
if (!(*status & flag))
|
|
|
|
ret = -ETIMEDOUT;
|
|
|
|
} else
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-12-17 08:40:48 +08:00
|
|
|
/* Clear saved irqstatus after using an interrupt */
|
2020-05-24 01:32:56 +08:00
|
|
|
static u8 hdq_reset_irqstatus(struct hdq_data *hdq_data, u8 bits)
|
2019-12-17 08:40:48 +08:00
|
|
|
{
|
|
|
|
unsigned long irqflags;
|
2020-05-24 01:32:56 +08:00
|
|
|
u8 status;
|
2019-12-17 08:40:48 +08:00
|
|
|
|
|
|
|
spin_lock_irqsave(&hdq_data->hdq_spinlock, irqflags);
|
2020-05-24 01:32:56 +08:00
|
|
|
status = hdq_data->hdq_irqstatus;
|
|
|
|
/* this is a read-modify-write */
|
|
|
|
hdq_data->hdq_irqstatus &= ~bits;
|
2019-12-17 08:40:48 +08:00
|
|
|
spin_unlock_irqrestore(&hdq_data->hdq_spinlock, irqflags);
|
2020-05-24 01:32:56 +08:00
|
|
|
|
|
|
|
return status;
|
2019-12-17 08:40:48 +08:00
|
|
|
}
|
|
|
|
|
2008-11-13 05:27:09 +08:00
|
|
|
/* write out a byte and fill *status with HDQ_INT_STATUS */
|
|
|
|
static int hdq_write_byte(struct hdq_data *hdq_data, u8 val, u8 *status)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
u8 tmp_status;
|
|
|
|
|
2020-05-24 01:32:56 +08:00
|
|
|
ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
|
|
|
|
if (ret < 0) {
|
|
|
|
ret = -EINTR;
|
|
|
|
goto rtn;
|
|
|
|
}
|
|
|
|
|
2020-05-24 01:32:57 +08:00
|
|
|
if (hdq_data->hdq_irqstatus)
|
|
|
|
dev_err(hdq_data->dev, "TX irqstatus not cleared (%02x)\n",
|
|
|
|
hdq_data->hdq_irqstatus);
|
|
|
|
|
2008-11-13 05:27:09 +08:00
|
|
|
*status = 0;
|
|
|
|
|
|
|
|
hdq_reg_out(hdq_data, OMAP_HDQ_TX_DATA, val);
|
|
|
|
|
|
|
|
/* set the GO bit */
|
|
|
|
hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS, OMAP_HDQ_CTRL_STATUS_GO,
|
|
|
|
OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_GO);
|
|
|
|
/* wait for the TXCOMPLETE bit */
|
|
|
|
ret = wait_event_timeout(hdq_wait_queue,
|
2020-05-24 01:32:56 +08:00
|
|
|
(hdq_data->hdq_irqstatus & OMAP_HDQ_INT_STATUS_TXCOMPLETE),
|
|
|
|
OMAP_HDQ_TIMEOUT);
|
|
|
|
*status = hdq_reset_irqstatus(hdq_data, OMAP_HDQ_INT_STATUS_TXCOMPLETE);
|
2008-11-13 05:27:09 +08:00
|
|
|
if (ret == 0) {
|
|
|
|
dev_dbg(hdq_data->dev, "TX wait elapsed\n");
|
2012-05-22 07:43:02 +08:00
|
|
|
ret = -ETIMEDOUT;
|
2008-11-13 05:27:09 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check irqstatus */
|
|
|
|
if (!(*status & OMAP_HDQ_INT_STATUS_TXCOMPLETE)) {
|
|
|
|
dev_dbg(hdq_data->dev, "timeout waiting for"
|
2020-05-24 01:32:54 +08:00
|
|
|
" TXCOMPLETE/RXCOMPLETE, %x\n", *status);
|
2008-11-13 05:27:09 +08:00
|
|
|
ret = -ETIMEDOUT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait for the GO bit return to zero */
|
|
|
|
ret = hdq_wait_for_flag(hdq_data, OMAP_HDQ_CTRL_STATUS,
|
|
|
|
OMAP_HDQ_CTRL_STATUS_GO,
|
|
|
|
OMAP_HDQ_FLAG_CLEAR, &tmp_status);
|
|
|
|
if (ret) {
|
|
|
|
dev_dbg(hdq_data->dev, "timeout waiting GO bit"
|
2020-05-24 01:32:54 +08:00
|
|
|
" return to zero, %x\n", tmp_status);
|
2008-11-13 05:27:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2020-05-24 01:32:56 +08:00
|
|
|
mutex_unlock(&hdq_data->hdq_mutex);
|
|
|
|
rtn:
|
2008-11-13 05:27:09 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* HDQ Interrupt service routine */
|
|
|
|
static irqreturn_t hdq_isr(int irq, void *_hdq)
|
|
|
|
{
|
|
|
|
struct hdq_data *hdq_data = _hdq;
|
|
|
|
unsigned long irqflags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&hdq_data->hdq_spinlock, irqflags);
|
2020-05-24 01:32:56 +08:00
|
|
|
hdq_data->hdq_irqstatus |= hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS);
|
2008-11-13 05:27:09 +08:00
|
|
|
spin_unlock_irqrestore(&hdq_data->hdq_spinlock, irqflags);
|
2020-05-24 01:32:54 +08:00
|
|
|
dev_dbg(hdq_data->dev, "hdq_isr: %x\n", hdq_data->hdq_irqstatus);
|
2008-11-13 05:27:09 +08:00
|
|
|
|
|
|
|
if (hdq_data->hdq_irqstatus &
|
|
|
|
(OMAP_HDQ_INT_STATUS_TXCOMPLETE | OMAP_HDQ_INT_STATUS_RXCOMPLETE
|
|
|
|
| OMAP_HDQ_INT_STATUS_TIMEOUT)) {
|
|
|
|
/* wake up sleeping process */
|
|
|
|
wake_up(&hdq_wait_queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2015-09-15 01:54:33 +08:00
|
|
|
/* W1 search callback function in HDQ mode */
|
2008-11-20 07:36:50 +08:00
|
|
|
static void omap_w1_search_bus(void *_hdq, struct w1_master *master_dev,
|
|
|
|
u8 search_type, w1_slave_found_callback slave_found)
|
2008-11-13 05:27:09 +08:00
|
|
|
{
|
|
|
|
u64 module_id, rn_le, cs, id;
|
|
|
|
|
|
|
|
if (w1_id)
|
|
|
|
module_id = w1_id;
|
|
|
|
else
|
|
|
|
module_id = 0x1;
|
|
|
|
|
|
|
|
rn_le = cpu_to_le64(module_id);
|
|
|
|
/*
|
|
|
|
* HDQ might not obey truly the 1-wire spec.
|
|
|
|
* So calculate CRC based on module parameter.
|
|
|
|
*/
|
|
|
|
cs = w1_calc_crc8((u8 *)&rn_le, 7);
|
|
|
|
id = (cs << 56) | module_id;
|
|
|
|
|
2008-11-20 07:36:50 +08:00
|
|
|
slave_found(master_dev, id);
|
2008-11-13 05:27:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Issue break pulse to the device */
|
|
|
|
static int omap_hdq_break(struct hdq_data *hdq_data)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
u8 tmp_status;
|
|
|
|
|
|
|
|
ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_dbg(hdq_data->dev, "Could not acquire mutex\n");
|
|
|
|
ret = -EINTR;
|
|
|
|
goto rtn;
|
|
|
|
}
|
|
|
|
|
2020-05-24 01:32:57 +08:00
|
|
|
if (hdq_data->hdq_irqstatus)
|
|
|
|
dev_err(hdq_data->dev, "break irqstatus not cleared (%02x)\n",
|
|
|
|
hdq_data->hdq_irqstatus);
|
|
|
|
|
2008-11-13 05:27:09 +08:00
|
|
|
/* set the INIT and GO bit */
|
|
|
|
hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS,
|
|
|
|
OMAP_HDQ_CTRL_STATUS_INITIALIZATION | OMAP_HDQ_CTRL_STATUS_GO,
|
|
|
|
OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_INITIALIZATION |
|
|
|
|
OMAP_HDQ_CTRL_STATUS_GO);
|
|
|
|
|
|
|
|
/* wait for the TIMEOUT bit */
|
|
|
|
ret = wait_event_timeout(hdq_wait_queue,
|
2020-05-24 01:32:56 +08:00
|
|
|
(hdq_data->hdq_irqstatus & OMAP_HDQ_INT_STATUS_TIMEOUT),
|
|
|
|
OMAP_HDQ_TIMEOUT);
|
|
|
|
tmp_status = hdq_reset_irqstatus(hdq_data, OMAP_HDQ_INT_STATUS_TIMEOUT);
|
2008-11-13 05:27:09 +08:00
|
|
|
if (ret == 0) {
|
|
|
|
dev_dbg(hdq_data->dev, "break wait elapsed\n");
|
|
|
|
ret = -EINTR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check irqstatus */
|
|
|
|
if (!(tmp_status & OMAP_HDQ_INT_STATUS_TIMEOUT)) {
|
2020-05-24 01:32:54 +08:00
|
|
|
dev_dbg(hdq_data->dev, "timeout waiting for TIMEOUT, %x\n",
|
2020-05-24 01:32:56 +08:00
|
|
|
tmp_status);
|
2008-11-13 05:27:09 +08:00
|
|
|
ret = -ETIMEDOUT;
|
|
|
|
goto out;
|
|
|
|
}
|
2015-09-15 01:54:33 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* check for the presence detect bit to get
|
|
|
|
* set to show that the slave is responding
|
|
|
|
*/
|
|
|
|
if (!(hdq_reg_in(hdq_data, OMAP_HDQ_CTRL_STATUS) &
|
|
|
|
OMAP_HDQ_CTRL_STATUS_PRESENCE)) {
|
|
|
|
dev_dbg(hdq_data->dev, "Presence bit not set\n");
|
|
|
|
ret = -ETIMEDOUT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2008-11-13 05:27:09 +08:00
|
|
|
/*
|
|
|
|
* wait for both INIT and GO bits rerurn to zero.
|
|
|
|
* zero wait time expected for interrupt mode.
|
|
|
|
*/
|
|
|
|
ret = hdq_wait_for_flag(hdq_data, OMAP_HDQ_CTRL_STATUS,
|
|
|
|
OMAP_HDQ_CTRL_STATUS_INITIALIZATION |
|
|
|
|
OMAP_HDQ_CTRL_STATUS_GO, OMAP_HDQ_FLAG_CLEAR,
|
|
|
|
&tmp_status);
|
|
|
|
if (ret)
|
|
|
|
dev_dbg(hdq_data->dev, "timeout waiting INIT&GO bits"
|
2020-05-24 01:32:54 +08:00
|
|
|
" return to zero, %x\n", tmp_status);
|
2008-11-13 05:27:09 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
mutex_unlock(&hdq_data->hdq_mutex);
|
|
|
|
rtn:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hdq_read_byte(struct hdq_data *hdq_data, u8 *val)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
u8 status;
|
|
|
|
|
|
|
|
ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
|
|
|
|
if (ret < 0) {
|
|
|
|
ret = -EINTR;
|
|
|
|
goto rtn;
|
|
|
|
}
|
|
|
|
|
2019-12-17 08:40:48 +08:00
|
|
|
if (pm_runtime_suspended(hdq_data->dev)) {
|
2008-11-13 05:27:09 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(hdq_data->hdq_irqstatus & OMAP_HDQ_INT_STATUS_RXCOMPLETE)) {
|
|
|
|
hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS,
|
|
|
|
OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_GO,
|
|
|
|
OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_GO);
|
|
|
|
/*
|
2012-05-22 07:43:02 +08:00
|
|
|
* The RX comes immediately after TX.
|
2008-11-13 05:27:09 +08:00
|
|
|
*/
|
2012-05-22 07:43:02 +08:00
|
|
|
wait_event_timeout(hdq_wait_queue,
|
|
|
|
(hdq_data->hdq_irqstatus
|
2020-05-24 01:32:56 +08:00
|
|
|
& (OMAP_HDQ_INT_STATUS_RXCOMPLETE |
|
|
|
|
OMAP_HDQ_INT_STATUS_TIMEOUT)),
|
2012-05-22 07:43:02 +08:00
|
|
|
OMAP_HDQ_TIMEOUT);
|
2020-05-24 01:32:56 +08:00
|
|
|
status = hdq_reset_irqstatus(hdq_data,
|
|
|
|
OMAP_HDQ_INT_STATUS_RXCOMPLETE |
|
|
|
|
OMAP_HDQ_INT_STATUS_TIMEOUT);
|
2008-11-13 05:27:09 +08:00
|
|
|
hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS, 0,
|
|
|
|
OMAP_HDQ_CTRL_STATUS_DIR);
|
2020-05-24 01:32:56 +08:00
|
|
|
|
2008-11-13 05:27:09 +08:00
|
|
|
/* check irqstatus */
|
|
|
|
if (!(status & OMAP_HDQ_INT_STATUS_RXCOMPLETE)) {
|
|
|
|
dev_dbg(hdq_data->dev, "timeout waiting for"
|
2012-05-22 07:43:02 +08:00
|
|
|
" RXCOMPLETE, %x", status);
|
2008-11-13 05:27:09 +08:00
|
|
|
ret = -ETIMEDOUT;
|
|
|
|
goto out;
|
|
|
|
}
|
2020-05-24 01:32:56 +08:00
|
|
|
} else { /* interrupt had occurred before hdq_read_byte was called */
|
|
|
|
hdq_reset_irqstatus(hdq_data, OMAP_HDQ_INT_STATUS_RXCOMPLETE);
|
2008-11-13 05:27:09 +08:00
|
|
|
}
|
|
|
|
/* the data is ready. Read it in! */
|
|
|
|
*val = hdq_reg_in(hdq_data, OMAP_HDQ_RX_DATA);
|
|
|
|
out:
|
|
|
|
mutex_unlock(&hdq_data->hdq_mutex);
|
|
|
|
rtn:
|
2012-05-22 07:43:02 +08:00
|
|
|
return ret;
|
2008-11-13 05:27:09 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-15 01:54:33 +08:00
|
|
|
/*
|
|
|
|
* W1 triplet callback function - used for searching ROM addresses.
|
|
|
|
* Registered only when controller is in 1-wire mode.
|
|
|
|
*/
|
|
|
|
static u8 omap_w1_triplet(void *_hdq, u8 bdir)
|
|
|
|
{
|
|
|
|
u8 id_bit, comp_bit;
|
|
|
|
int err;
|
|
|
|
u8 ret = 0x3; /* no slaves responded */
|
|
|
|
struct hdq_data *hdq_data = _hdq;
|
|
|
|
u8 ctrl = OMAP_HDQ_CTRL_STATUS_SINGLE | OMAP_HDQ_CTRL_STATUS_GO |
|
|
|
|
OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK;
|
|
|
|
u8 mask = ctrl | OMAP_HDQ_CTRL_STATUS_DIR;
|
|
|
|
|
2019-12-17 08:40:48 +08:00
|
|
|
err = pm_runtime_get_sync(hdq_data->dev);
|
|
|
|
if (err < 0) {
|
|
|
|
pm_runtime_put_noidle(hdq_data->dev);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2015-09-15 01:54:33 +08:00
|
|
|
|
|
|
|
err = mutex_lock_interruptible(&hdq_data->hdq_mutex);
|
|
|
|
if (err < 0) {
|
|
|
|
dev_dbg(hdq_data->dev, "Could not acquire mutex\n");
|
|
|
|
goto rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read id_bit */
|
|
|
|
hdq_reg_merge(_hdq, OMAP_HDQ_CTRL_STATUS,
|
|
|
|
ctrl | OMAP_HDQ_CTRL_STATUS_DIR, mask);
|
|
|
|
err = wait_event_timeout(hdq_wait_queue,
|
|
|
|
(hdq_data->hdq_irqstatus
|
|
|
|
& OMAP_HDQ_INT_STATUS_RXCOMPLETE),
|
|
|
|
OMAP_HDQ_TIMEOUT);
|
2020-05-24 01:32:56 +08:00
|
|
|
/* Must clear irqstatus for another RXCOMPLETE interrupt */
|
|
|
|
hdq_reset_irqstatus(hdq_data, OMAP_HDQ_INT_STATUS_RXCOMPLETE);
|
|
|
|
|
2015-09-15 01:54:33 +08:00
|
|
|
if (err == 0) {
|
|
|
|
dev_dbg(hdq_data->dev, "RX wait elapsed\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
id_bit = (hdq_reg_in(_hdq, OMAP_HDQ_RX_DATA) & 0x01);
|
|
|
|
|
|
|
|
/* read comp_bit */
|
|
|
|
hdq_reg_merge(_hdq, OMAP_HDQ_CTRL_STATUS,
|
|
|
|
ctrl | OMAP_HDQ_CTRL_STATUS_DIR, mask);
|
|
|
|
err = wait_event_timeout(hdq_wait_queue,
|
|
|
|
(hdq_data->hdq_irqstatus
|
|
|
|
& OMAP_HDQ_INT_STATUS_RXCOMPLETE),
|
|
|
|
OMAP_HDQ_TIMEOUT);
|
2020-05-24 01:32:56 +08:00
|
|
|
/* Must clear irqstatus for another RXCOMPLETE interrupt */
|
|
|
|
hdq_reset_irqstatus(hdq_data, OMAP_HDQ_INT_STATUS_RXCOMPLETE);
|
|
|
|
|
2015-09-15 01:54:33 +08:00
|
|
|
if (err == 0) {
|
|
|
|
dev_dbg(hdq_data->dev, "RX wait elapsed\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
comp_bit = (hdq_reg_in(_hdq, OMAP_HDQ_RX_DATA) & 0x01);
|
|
|
|
|
|
|
|
if (id_bit && comp_bit) {
|
|
|
|
ret = 0x03; /* no slaves responded */
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (!id_bit && !comp_bit) {
|
|
|
|
/* Both bits are valid, take the direction given */
|
|
|
|
ret = bdir ? 0x04 : 0;
|
|
|
|
} else {
|
|
|
|
/* Only one bit is valid, take that direction */
|
|
|
|
bdir = id_bit;
|
|
|
|
ret = id_bit ? 0x05 : 0x02;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write bdir bit */
|
|
|
|
hdq_reg_out(_hdq, OMAP_HDQ_TX_DATA, bdir);
|
|
|
|
hdq_reg_merge(_hdq, OMAP_HDQ_CTRL_STATUS, ctrl, mask);
|
|
|
|
err = wait_event_timeout(hdq_wait_queue,
|
|
|
|
(hdq_data->hdq_irqstatus
|
|
|
|
& OMAP_HDQ_INT_STATUS_TXCOMPLETE),
|
|
|
|
OMAP_HDQ_TIMEOUT);
|
2020-05-24 01:32:56 +08:00
|
|
|
/* Must clear irqstatus for another TXCOMPLETE interrupt */
|
|
|
|
hdq_reset_irqstatus(hdq_data, OMAP_HDQ_INT_STATUS_TXCOMPLETE);
|
|
|
|
|
2015-09-15 01:54:33 +08:00
|
|
|
if (err == 0) {
|
|
|
|
dev_dbg(hdq_data->dev, "TX wait elapsed\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
hdq_reg_merge(_hdq, OMAP_HDQ_CTRL_STATUS, 0,
|
|
|
|
OMAP_HDQ_CTRL_STATUS_SINGLE);
|
|
|
|
|
|
|
|
out:
|
|
|
|
mutex_unlock(&hdq_data->hdq_mutex);
|
|
|
|
rtn:
|
2019-12-17 08:40:48 +08:00
|
|
|
pm_runtime_mark_last_busy(hdq_data->dev);
|
|
|
|
pm_runtime_put_autosuspend(hdq_data->dev);
|
|
|
|
|
2015-09-15 01:54:33 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reset callback */
|
|
|
|
static u8 omap_w1_reset_bus(void *_hdq)
|
|
|
|
{
|
2019-12-17 08:40:48 +08:00
|
|
|
struct hdq_data *hdq_data = _hdq;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = pm_runtime_get_sync(hdq_data->dev);
|
|
|
|
if (err < 0) {
|
|
|
|
pm_runtime_put_noidle(hdq_data->dev);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
omap_hdq_break(hdq_data);
|
|
|
|
|
|
|
|
pm_runtime_mark_last_busy(hdq_data->dev);
|
|
|
|
pm_runtime_put_autosuspend(hdq_data->dev);
|
|
|
|
|
2015-09-15 01:54:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-13 05:27:09 +08:00
|
|
|
/* Read a byte of data from the device */
|
|
|
|
static u8 omap_w1_read_byte(void *_hdq)
|
|
|
|
{
|
|
|
|
struct hdq_data *hdq_data = _hdq;
|
|
|
|
u8 val = 0;
|
|
|
|
int ret;
|
|
|
|
|
2019-12-17 08:40:48 +08:00
|
|
|
ret = pm_runtime_get_sync(hdq_data->dev);
|
|
|
|
if (ret < 0) {
|
|
|
|
pm_runtime_put_noidle(hdq_data->dev);
|
2015-09-15 01:54:33 +08:00
|
|
|
|
2008-11-13 05:27:09 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-12-17 08:40:48 +08:00
|
|
|
ret = hdq_read_byte(hdq_data, &val);
|
|
|
|
if (ret)
|
2020-05-24 01:32:55 +08:00
|
|
|
val = -1;
|
2015-09-15 01:54:33 +08:00
|
|
|
|
2019-12-17 08:40:48 +08:00
|
|
|
pm_runtime_mark_last_busy(hdq_data->dev);
|
|
|
|
pm_runtime_put_autosuspend(hdq_data->dev);
|
2008-11-13 05:27:09 +08:00
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write a byte of data to the device */
|
|
|
|
static void omap_w1_write_byte(void *_hdq, u8 byte)
|
|
|
|
{
|
|
|
|
struct hdq_data *hdq_data = _hdq;
|
|
|
|
int ret;
|
|
|
|
u8 status;
|
|
|
|
|
2019-12-17 08:40:48 +08:00
|
|
|
ret = pm_runtime_get_sync(hdq_data->dev);
|
|
|
|
if (ret < 0) {
|
|
|
|
pm_runtime_put_noidle(hdq_data->dev);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2008-11-13 05:27:09 +08:00
|
|
|
|
2015-09-15 01:54:33 +08:00
|
|
|
/*
|
|
|
|
* We need to reset the slave before
|
|
|
|
* issuing the SKIP ROM command, else
|
|
|
|
* the slave will not work.
|
|
|
|
*/
|
|
|
|
if (byte == W1_SKIP_ROM)
|
|
|
|
omap_hdq_break(hdq_data);
|
|
|
|
|
2008-11-13 05:27:09 +08:00
|
|
|
ret = hdq_write_byte(hdq_data, byte, &status);
|
2012-05-22 07:43:02 +08:00
|
|
|
if (ret < 0) {
|
2008-11-13 05:27:09 +08:00
|
|
|
dev_dbg(hdq_data->dev, "TX failure:Ctrl status %x\n", status);
|
2019-12-17 08:40:48 +08:00
|
|
|
goto out_err;
|
2008-11-13 05:27:09 +08:00
|
|
|
}
|
|
|
|
|
2019-12-17 08:40:48 +08:00
|
|
|
out_err:
|
|
|
|
pm_runtime_mark_last_busy(hdq_data->dev);
|
|
|
|
pm_runtime_put_autosuspend(hdq_data->dev);
|
2008-11-13 05:27:09 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 04:02:12 +08:00
|
|
|
static struct w1_bus_master omap_w1_master = {
|
|
|
|
.read_byte = omap_w1_read_byte,
|
|
|
|
.write_byte = omap_w1_write_byte,
|
|
|
|
.reset_bus = omap_w1_reset_bus,
|
|
|
|
};
|
|
|
|
|
2019-12-17 08:40:48 +08:00
|
|
|
static int __maybe_unused omap_hdq_runtime_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct hdq_data *hdq_data = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
hdq_reg_out(hdq_data, 0, hdq_data->mode);
|
|
|
|
hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __maybe_unused omap_hdq_runtime_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct hdq_data *hdq_data = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
/* select HDQ/1W mode & enable clocks */
|
|
|
|
hdq_reg_out(hdq_data, OMAP_HDQ_CTRL_STATUS,
|
|
|
|
OMAP_HDQ_CTRL_STATUS_CLOCKENABLE |
|
|
|
|
OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK |
|
|
|
|
hdq_data->mode);
|
|
|
|
hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dev_pm_ops omap_hdq_pm_ops = {
|
|
|
|
SET_RUNTIME_PM_OPS(omap_hdq_runtime_suspend,
|
|
|
|
omap_hdq_runtime_resume, NULL)
|
|
|
|
};
|
|
|
|
|
2012-11-20 02:21:43 +08:00
|
|
|
static int omap_hdq_probe(struct platform_device *pdev)
|
2008-11-13 05:27:09 +08:00
|
|
|
{
|
2012-07-25 20:05:30 +08:00
|
|
|
struct device *dev = &pdev->dev;
|
2008-11-13 05:27:09 +08:00
|
|
|
struct hdq_data *hdq_data;
|
|
|
|
int ret, irq;
|
|
|
|
u8 rev;
|
2015-09-15 01:54:33 +08:00
|
|
|
const char *mode;
|
2008-11-13 05:27:09 +08:00
|
|
|
|
2012-07-25 20:05:30 +08:00
|
|
|
hdq_data = devm_kzalloc(dev, sizeof(*hdq_data), GFP_KERNEL);
|
2023-04-15 18:43:03 +08:00
|
|
|
if (!hdq_data)
|
2012-07-25 20:05:30 +08:00
|
|
|
return -ENOMEM;
|
2008-11-13 05:27:09 +08:00
|
|
|
|
2012-07-25 20:05:30 +08:00
|
|
|
hdq_data->dev = dev;
|
2008-11-13 05:27:09 +08:00
|
|
|
platform_set_drvdata(pdev, hdq_data);
|
|
|
|
|
2019-08-02 21:50:10 +08:00
|
|
|
hdq_data->hdq_base = devm_platform_ioremap_resource(pdev, 0);
|
2013-01-21 18:09:24 +08:00
|
|
|
if (IS_ERR(hdq_data->hdq_base))
|
|
|
|
return PTR_ERR(hdq_data->hdq_base);
|
2008-11-13 05:27:09 +08:00
|
|
|
|
|
|
|
mutex_init(&hdq_data->hdq_mutex);
|
|
|
|
|
2019-12-17 08:40:48 +08:00
|
|
|
ret = of_property_read_string(pdev->dev.of_node, "ti,mode", &mode);
|
|
|
|
if (ret < 0 || !strcmp(mode, "hdq")) {
|
|
|
|
hdq_data->mode = 0;
|
|
|
|
omap_w1_master.search = omap_w1_search_bus;
|
|
|
|
} else {
|
|
|
|
hdq_data->mode = 1;
|
|
|
|
omap_w1_master.triplet = omap_w1_triplet;
|
|
|
|
}
|
|
|
|
|
2012-06-22 11:40:40 +08:00
|
|
|
pm_runtime_enable(&pdev->dev);
|
2019-12-17 08:40:48 +08:00
|
|
|
pm_runtime_use_autosuspend(&pdev->dev);
|
|
|
|
pm_runtime_set_autosuspend_delay(&pdev->dev, 300);
|
2015-09-15 01:54:33 +08:00
|
|
|
ret = pm_runtime_get_sync(&pdev->dev);
|
|
|
|
if (ret < 0) {
|
2019-12-17 08:40:48 +08:00
|
|
|
pm_runtime_put_noidle(&pdev->dev);
|
2015-09-15 01:54:33 +08:00
|
|
|
dev_dbg(&pdev->dev, "pm_runtime_get_sync failed\n");
|
|
|
|
goto err_w1;
|
|
|
|
}
|
|
|
|
|
2008-11-13 05:27:09 +08:00
|
|
|
rev = hdq_reg_in(hdq_data, OMAP_HDQ_REVISION);
|
|
|
|
dev_info(&pdev->dev, "OMAP HDQ Hardware Rev %c.%c. Driver in %s mode\n",
|
|
|
|
(rev >> 4) + '0', (rev & 0x0f) + '0', "Interrupt");
|
|
|
|
|
|
|
|
spin_lock_init(&hdq_data->hdq_spinlock);
|
|
|
|
|
|
|
|
irq = platform_get_irq(pdev, 0);
|
|
|
|
if (irq < 0) {
|
2017-07-01 06:44:02 +08:00
|
|
|
dev_dbg(&pdev->dev, "Failed to get IRQ: %d\n", irq);
|
|
|
|
ret = irq;
|
2008-11-13 05:27:09 +08:00
|
|
|
goto err_irq;
|
|
|
|
}
|
|
|
|
|
2013-10-13 15:20:05 +08:00
|
|
|
ret = devm_request_irq(dev, irq, hdq_isr, 0, "omap_hdq", hdq_data);
|
2008-11-13 05:27:09 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
dev_dbg(&pdev->dev, "could not request irq\n");
|
|
|
|
goto err_irq;
|
|
|
|
}
|
|
|
|
|
|
|
|
omap_hdq_break(hdq_data);
|
|
|
|
|
2019-12-17 08:40:48 +08:00
|
|
|
pm_runtime_mark_last_busy(&pdev->dev);
|
|
|
|
pm_runtime_put_autosuspend(&pdev->dev);
|
2015-09-15 01:54:33 +08:00
|
|
|
|
2008-11-13 05:27:09 +08:00
|
|
|
omap_w1_master.data = hdq_data;
|
|
|
|
|
|
|
|
ret = w1_add_master_device(&omap_w1_master);
|
|
|
|
if (ret) {
|
|
|
|
dev_dbg(&pdev->dev, "Failure in registering w1 master\n");
|
|
|
|
goto err_w1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_irq:
|
2012-06-22 11:40:40 +08:00
|
|
|
pm_runtime_put_sync(&pdev->dev);
|
|
|
|
err_w1:
|
2019-12-17 08:40:48 +08:00
|
|
|
pm_runtime_dont_use_autosuspend(&pdev->dev);
|
2012-06-22 11:40:40 +08:00
|
|
|
pm_runtime_disable(&pdev->dev);
|
2011-02-11 07:01:37 +08:00
|
|
|
|
2008-11-13 05:27:09 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-20 02:26:23 +08:00
|
|
|
static int omap_hdq_remove(struct platform_device *pdev)
|
2008-11-13 05:27:09 +08:00
|
|
|
{
|
2019-12-17 08:40:48 +08:00
|
|
|
int active;
|
2008-11-13 05:27:09 +08:00
|
|
|
|
2019-12-17 08:40:48 +08:00
|
|
|
active = pm_runtime_get_sync(&pdev->dev);
|
|
|
|
if (active < 0)
|
|
|
|
pm_runtime_put_noidle(&pdev->dev);
|
2008-11-13 05:27:09 +08:00
|
|
|
|
2019-12-17 08:40:48 +08:00
|
|
|
w1_remove_master_device(&omap_w1_master);
|
2008-11-13 05:27:09 +08:00
|
|
|
|
2019-12-17 08:40:48 +08:00
|
|
|
pm_runtime_dont_use_autosuspend(&pdev->dev);
|
|
|
|
if (active >= 0)
|
|
|
|
pm_runtime_put_sync(&pdev->dev);
|
2012-06-22 11:40:40 +08:00
|
|
|
pm_runtime_disable(&pdev->dev);
|
2008-11-13 05:27:09 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-17 04:02:12 +08:00
|
|
|
static const struct of_device_id omap_hdq_dt_ids[] = {
|
|
|
|
{ .compatible = "ti,omap3-1w" },
|
|
|
|
{ .compatible = "ti,am4372-hdq" },
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, omap_hdq_dt_ids);
|
2008-11-13 05:27:09 +08:00
|
|
|
|
2017-05-17 04:02:12 +08:00
|
|
|
static struct platform_driver omap_hdq_driver = {
|
|
|
|
.probe = omap_hdq_probe,
|
|
|
|
.remove = omap_hdq_remove,
|
|
|
|
.driver = {
|
|
|
|
.name = "omap_hdq",
|
|
|
|
.of_match_table = omap_hdq_dt_ids,
|
2019-12-17 08:40:48 +08:00
|
|
|
.pm = &omap_hdq_pm_ops,
|
2017-05-17 04:02:12 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
module_platform_driver(omap_hdq_driver);
|
2008-11-13 05:27:09 +08:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Texas Instruments");
|
2015-09-15 01:54:33 +08:00
|
|
|
MODULE_DESCRIPTION("HDQ-1W driver Library");
|
2008-11-13 05:27:09 +08:00
|
|
|
MODULE_LICENSE("GPL");
|