2019-05-27 13:55:06 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2009-01-08 09:08:58 +07:00
|
|
|
/*
|
|
|
|
* Copyright 2005-2008 Freescale Semiconductor, Inc. All Rights Reserved.
|
|
|
|
* Copyright 2008 Luotao Fu, kernel@pengutronix.de
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/io.h>
|
2014-05-08 14:56:38 +07:00
|
|
|
#include <linux/jiffies.h>
|
2014-02-22 14:29:50 +07:00
|
|
|
#include <linux/module.h>
|
2018-06-20 12:47:28 +07:00
|
|
|
#include <linux/mod_devicetable.h>
|
2014-02-22 14:29:50 +07:00
|
|
|
#include <linux/platform_device.h>
|
2009-01-08 09:08:58 +07:00
|
|
|
|
2017-06-05 20:52:08 +07:00
|
|
|
#include <linux/w1.h>
|
2009-01-08 09:08:58 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* MXC W1 Register offsets
|
|
|
|
*/
|
2014-02-22 14:29:50 +07:00
|
|
|
#define MXC_W1_CONTROL 0x00
|
|
|
|
# define MXC_W1_CONTROL_RDST BIT(3)
|
|
|
|
# define MXC_W1_CONTROL_WR(x) BIT(5 - (x))
|
|
|
|
# define MXC_W1_CONTROL_PST BIT(6)
|
|
|
|
# define MXC_W1_CONTROL_RPP BIT(7)
|
|
|
|
#define MXC_W1_TIME_DIVIDER 0x02
|
|
|
|
#define MXC_W1_RESET 0x04
|
2014-05-08 14:56:39 +07:00
|
|
|
# define MXC_W1_RESET_RST BIT(0)
|
2009-01-08 09:08:58 +07:00
|
|
|
|
|
|
|
struct mxc_w1_device {
|
|
|
|
void __iomem *regs;
|
|
|
|
struct clk *clk;
|
|
|
|
struct w1_bus_master bus_master;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this is the low level routine to
|
|
|
|
* reset the device on the One Wire interface
|
|
|
|
* on the hardware
|
|
|
|
*/
|
|
|
|
static u8 mxc_w1_ds2_reset_bus(void *data)
|
|
|
|
{
|
|
|
|
struct mxc_w1_device *dev = data;
|
2014-05-08 14:56:38 +07:00
|
|
|
unsigned long timeout;
|
2009-01-08 09:08:58 +07:00
|
|
|
|
2014-05-08 14:56:38 +07:00
|
|
|
writeb(MXC_W1_CONTROL_RPP, dev->regs + MXC_W1_CONTROL);
|
2009-01-08 09:08:58 +07:00
|
|
|
|
2014-05-08 14:56:38 +07:00
|
|
|
/* Wait for reset sequence 511+512us, use 1500us for sure */
|
|
|
|
timeout = jiffies + usecs_to_jiffies(1500);
|
2009-01-08 09:08:58 +07:00
|
|
|
|
2014-05-08 14:56:38 +07:00
|
|
|
udelay(511 + 512);
|
2009-01-08 09:08:58 +07:00
|
|
|
|
2014-05-08 14:56:38 +07:00
|
|
|
do {
|
|
|
|
u8 ctrl = readb(dev->regs + MXC_W1_CONTROL);
|
|
|
|
|
|
|
|
/* PST bit is valid after the RPP bit is self-cleared */
|
|
|
|
if (!(ctrl & MXC_W1_CONTROL_RPP))
|
|
|
|
return !(ctrl & MXC_W1_CONTROL_PST);
|
|
|
|
} while (time_is_after_jiffies(timeout));
|
|
|
|
|
|
|
|
return 1;
|
2009-01-08 09:08:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this is the low level routine to read/write a bit on the One Wire
|
|
|
|
* interface on the hardware. It does write 0 if parameter bit is set
|
|
|
|
* to 0, otherwise a write 1/read.
|
|
|
|
*/
|
|
|
|
static u8 mxc_w1_ds2_touch_bit(void *data, u8 bit)
|
|
|
|
{
|
2014-05-08 14:56:40 +07:00
|
|
|
struct mxc_w1_device *dev = data;
|
|
|
|
unsigned long timeout;
|
2009-01-08 09:08:58 +07:00
|
|
|
|
2014-05-08 14:56:40 +07:00
|
|
|
writeb(MXC_W1_CONTROL_WR(bit), dev->regs + MXC_W1_CONTROL);
|
2009-01-08 09:08:58 +07:00
|
|
|
|
2014-05-08 14:56:40 +07:00
|
|
|
/* Wait for read/write bit (60us, Max 120us), use 200us for sure */
|
|
|
|
timeout = jiffies + usecs_to_jiffies(200);
|
2009-01-08 09:08:58 +07:00
|
|
|
|
2014-05-08 14:56:40 +07:00
|
|
|
udelay(60);
|
|
|
|
|
|
|
|
do {
|
|
|
|
u8 ctrl = readb(dev->regs + MXC_W1_CONTROL);
|
|
|
|
|
|
|
|
/* RDST bit is valid after the WR1/RD bit is self-cleared */
|
|
|
|
if (!(ctrl & MXC_W1_CONTROL_WR(bit)))
|
|
|
|
return !!(ctrl & MXC_W1_CONTROL_RDST);
|
|
|
|
} while (time_is_after_jiffies(timeout));
|
2009-01-08 09:08:58 +07:00
|
|
|
|
2014-05-08 14:56:40 +07:00
|
|
|
return 0;
|
2009-01-08 09:08:58 +07:00
|
|
|
}
|
|
|
|
|
2012-11-20 01:21:43 +07:00
|
|
|
static int mxc_w1_probe(struct platform_device *pdev)
|
2009-01-08 09:08:58 +07:00
|
|
|
{
|
|
|
|
struct mxc_w1_device *mdev;
|
2013-11-29 18:39:29 +07:00
|
|
|
unsigned long clkrate;
|
2009-01-08 09:08:58 +07:00
|
|
|
struct resource *res;
|
2013-11-29 18:39:28 +07:00
|
|
|
unsigned int clkdiv;
|
2013-11-29 18:39:30 +07:00
|
|
|
int err;
|
2009-01-08 09:08:58 +07:00
|
|
|
|
drivers/w1/masters/mxc_w1.c: use devm_ functions
The various devm_ functions allocate memory that is released when a driver
detaches. This patch uses these functions for data that is allocated in
the probe function of a platform device and is only freed in the remove
function.
At the same time, this fixes two faults. First, mdev, the result of
kzalloc, was never freed. Second, on failure of ioremap, 0 was returned.
This has been replaced by -EBUSY, which was the failure value for the call
to request_mem_region, with which the call to ioremap has been combined.
The warning message on failure of ioremap is dropped, because
devm_request_and_ioremap already gives such messages on failure.
Finally, the initial call to platform_get_resource is moved closer to the
call to devm_request_and_ioremap, which takes care of checking whether its
result is NULL, implying that a test on the result of this call to
platform_get_resource is not needed.
Signed-off-by: Julia Lawall <Julia.Lawall@lip6.fr>
Acked-by: Evgeniy Polyakov <zbr@ioremap.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2012-12-07 06:15:24 +07:00
|
|
|
mdev = devm_kzalloc(&pdev->dev, sizeof(struct mxc_w1_device),
|
|
|
|
GFP_KERNEL);
|
2009-01-08 09:08:58 +07:00
|
|
|
if (!mdev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
drivers/w1/masters/mxc_w1.c: use devm_ functions
The various devm_ functions allocate memory that is released when a driver
detaches. This patch uses these functions for data that is allocated in
the probe function of a platform device and is only freed in the remove
function.
At the same time, this fixes two faults. First, mdev, the result of
kzalloc, was never freed. Second, on failure of ioremap, 0 was returned.
This has been replaced by -EBUSY, which was the failure value for the call
to request_mem_region, with which the call to ioremap has been combined.
The warning message on failure of ioremap is dropped, because
devm_request_and_ioremap already gives such messages on failure.
Finally, the initial call to platform_get_resource is moved closer to the
call to devm_request_and_ioremap, which takes care of checking whether its
result is NULL, implying that a test on the result of this call to
platform_get_resource is not needed.
Signed-off-by: Julia Lawall <Julia.Lawall@lip6.fr>
Acked-by: Evgeniy Polyakov <zbr@ioremap.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2012-12-07 06:15:24 +07:00
|
|
|
mdev->clk = devm_clk_get(&pdev->dev, NULL);
|
|
|
|
if (IS_ERR(mdev->clk))
|
|
|
|
return PTR_ERR(mdev->clk);
|
2009-01-08 09:08:58 +07:00
|
|
|
|
2018-05-02 15:55:31 +07:00
|
|
|
err = clk_prepare_enable(mdev->clk);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2013-11-29 18:39:29 +07:00
|
|
|
clkrate = clk_get_rate(mdev->clk);
|
|
|
|
if (clkrate < 10000000)
|
|
|
|
dev_warn(&pdev->dev,
|
|
|
|
"Low clock frequency causes improper function\n");
|
|
|
|
|
|
|
|
clkdiv = DIV_ROUND_CLOSEST(clkrate, 1000000);
|
|
|
|
clkrate /= clkdiv;
|
|
|
|
if ((clkrate < 980000) || (clkrate > 1020000))
|
|
|
|
dev_warn(&pdev->dev,
|
|
|
|
"Incorrect time base frequency %lu Hz\n", clkrate);
|
2009-01-08 09:08:58 +07:00
|
|
|
|
drivers/w1/masters/mxc_w1.c: use devm_ functions
The various devm_ functions allocate memory that is released when a driver
detaches. This patch uses these functions for data that is allocated in
the probe function of a platform device and is only freed in the remove
function.
At the same time, this fixes two faults. First, mdev, the result of
kzalloc, was never freed. Second, on failure of ioremap, 0 was returned.
This has been replaced by -EBUSY, which was the failure value for the call
to request_mem_region, with which the call to ioremap has been combined.
The warning message on failure of ioremap is dropped, because
devm_request_and_ioremap already gives such messages on failure.
Finally, the initial call to platform_get_resource is moved closer to the
call to devm_request_and_ioremap, which takes care of checking whether its
result is NULL, implying that a test on the result of this call to
platform_get_resource is not needed.
Signed-off-by: Julia Lawall <Julia.Lawall@lip6.fr>
Acked-by: Evgeniy Polyakov <zbr@ioremap.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2012-12-07 06:15:24 +07:00
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
2013-03-12 06:18:15 +07:00
|
|
|
mdev->regs = devm_ioremap_resource(&pdev->dev, res);
|
2018-05-02 15:55:31 +07:00
|
|
|
if (IS_ERR(mdev->regs)) {
|
|
|
|
err = PTR_ERR(mdev->regs);
|
|
|
|
goto out_disable_clk;
|
|
|
|
}
|
2013-11-29 18:39:30 +07:00
|
|
|
|
2014-05-08 14:56:39 +07:00
|
|
|
/* Software reset 1-Wire module */
|
|
|
|
writeb(MXC_W1_RESET_RST, mdev->regs + MXC_W1_RESET);
|
|
|
|
writeb(0, mdev->regs + MXC_W1_RESET);
|
|
|
|
|
2014-02-22 14:29:51 +07:00
|
|
|
writeb(clkdiv - 1, mdev->regs + MXC_W1_TIME_DIVIDER);
|
2009-01-08 09:08:58 +07:00
|
|
|
|
|
|
|
mdev->bus_master.data = mdev;
|
|
|
|
mdev->bus_master.reset_bus = mxc_w1_ds2_reset_bus;
|
|
|
|
mdev->bus_master.touch_bit = mxc_w1_ds2_touch_bit;
|
|
|
|
|
2013-11-29 18:39:30 +07:00
|
|
|
platform_set_drvdata(pdev, mdev);
|
2009-01-08 09:08:58 +07:00
|
|
|
|
2013-11-29 18:39:30 +07:00
|
|
|
err = w1_add_master_device(&mdev->bus_master);
|
2009-01-08 09:08:58 +07:00
|
|
|
if (err)
|
2018-05-02 15:55:31 +07:00
|
|
|
goto out_disable_clk;
|
2009-01-08 09:08:58 +07:00
|
|
|
|
2018-05-02 15:55:31 +07:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_disable_clk:
|
|
|
|
clk_disable_unprepare(mdev->clk);
|
2013-11-29 18:39:30 +07:00
|
|
|
return err;
|
2009-01-08 09:08:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* disassociate the w1 device from the driver
|
|
|
|
*/
|
2012-11-20 01:26:23 +07:00
|
|
|
static int mxc_w1_remove(struct platform_device *pdev)
|
2009-01-08 09:08:58 +07:00
|
|
|
{
|
|
|
|
struct mxc_w1_device *mdev = platform_get_drvdata(pdev);
|
|
|
|
|
|
|
|
w1_remove_master_device(&mdev->bus_master);
|
|
|
|
|
2012-03-08 02:59:36 +07:00
|
|
|
clk_disable_unprepare(mdev->clk);
|
2009-01-08 09:08:58 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-17 02:20:29 +07:00
|
|
|
static const struct of_device_id mxc_w1_dt_ids[] = {
|
2013-01-29 22:46:10 +07:00
|
|
|
{ .compatible = "fsl,imx21-owire" },
|
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, mxc_w1_dt_ids);
|
|
|
|
|
2009-01-08 09:08:58 +07:00
|
|
|
static struct platform_driver mxc_w1_driver = {
|
|
|
|
.driver = {
|
2013-01-29 22:46:10 +07:00
|
|
|
.name = "mxc_w1",
|
|
|
|
.of_match_table = mxc_w1_dt_ids,
|
2009-01-08 09:08:58 +07:00
|
|
|
},
|
|
|
|
.probe = mxc_w1_probe,
|
2012-12-22 03:55:26 +07:00
|
|
|
.remove = mxc_w1_remove,
|
2009-01-08 09:08:58 +07:00
|
|
|
};
|
2012-11-19 19:19:48 +07:00
|
|
|
module_platform_driver(mxc_w1_driver);
|
2009-01-08 09:08:58 +07:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Freescale Semiconductors Inc");
|
|
|
|
MODULE_DESCRIPTION("Driver for One-Wire on MXC");
|