2019-05-27 14:55:06 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2009-01-08 10:08:58 +08: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>
|
2020-09-30 16:36:46 +08:00
|
|
|
#include <linux/ktime.h>
|
2014-02-22 15:29:50 +08:00
|
|
|
#include <linux/module.h>
|
2018-06-20 13:47:28 +08:00
|
|
|
#include <linux/mod_devicetable.h>
|
2014-02-22 15:29:50 +08:00
|
|
|
#include <linux/platform_device.h>
|
2009-01-08 10:08:58 +08:00
|
|
|
|
2017-06-05 21:52:08 +08:00
|
|
|
#include <linux/w1.h>
|
2009-01-08 10:08:58 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* MXC W1 Register offsets
|
|
|
|
*/
|
2014-02-22 15:29:50 +08: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 15:56:39 +08:00
|
|
|
# define MXC_W1_RESET_RST BIT(0)
|
2009-01-08 10:08:58 +08: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;
|
2020-09-30 16:36:46 +08:00
|
|
|
ktime_t timeout;
|
2009-01-08 10:08:58 +08:00
|
|
|
|
2014-05-08 15:56:38 +08:00
|
|
|
writeb(MXC_W1_CONTROL_RPP, dev->regs + MXC_W1_CONTROL);
|
2009-01-08 10:08:58 +08:00
|
|
|
|
2014-05-08 15:56:38 +08:00
|
|
|
/* Wait for reset sequence 511+512us, use 1500us for sure */
|
2020-09-30 16:36:46 +08:00
|
|
|
timeout = ktime_add_us(ktime_get(), 1500);
|
2009-01-08 10:08:58 +08:00
|
|
|
|
2014-05-08 15:56:38 +08:00
|
|
|
udelay(511 + 512);
|
2009-01-08 10:08:58 +08:00
|
|
|
|
2014-05-08 15:56:38 +08: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);
|
2020-09-30 16:36:46 +08:00
|
|
|
} while (ktime_before(ktime_get(), timeout));
|
2014-05-08 15:56:38 +08:00
|
|
|
|
|
|
|
return 1;
|
2009-01-08 10:08:58 +08: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 15:56:40 +08:00
|
|
|
struct mxc_w1_device *dev = data;
|
2020-09-30 16:36:46 +08:00
|
|
|
ktime_t timeout;
|
2009-01-08 10:08:58 +08:00
|
|
|
|
2014-05-08 15:56:40 +08:00
|
|
|
writeb(MXC_W1_CONTROL_WR(bit), dev->regs + MXC_W1_CONTROL);
|
2009-01-08 10:08:58 +08:00
|
|
|
|
2014-05-08 15:56:40 +08:00
|
|
|
/* Wait for read/write bit (60us, Max 120us), use 200us for sure */
|
2020-09-30 16:36:46 +08:00
|
|
|
timeout = ktime_add_us(ktime_get(), 200);
|
2009-01-08 10:08:58 +08:00
|
|
|
|
2014-05-08 15:56:40 +08: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);
|
2020-09-30 16:36:46 +08:00
|
|
|
} while (ktime_before(ktime_get(), timeout));
|
2009-01-08 10:08:58 +08:00
|
|
|
|
2014-05-08 15:56:40 +08:00
|
|
|
return 0;
|
2009-01-08 10:08:58 +08:00
|
|
|
}
|
|
|
|
|
2012-11-20 02:21:43 +08:00
|
|
|
static int mxc_w1_probe(struct platform_device *pdev)
|
2009-01-08 10:08:58 +08:00
|
|
|
{
|
|
|
|
struct mxc_w1_device *mdev;
|
2013-11-29 19:39:29 +08:00
|
|
|
unsigned long clkrate;
|
2013-11-29 19:39:28 +08:00
|
|
|
unsigned int clkdiv;
|
2013-11-29 19:39:30 +08:00
|
|
|
int err;
|
2009-01-08 10:08:58 +08: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 07:15:24 +08:00
|
|
|
mdev = devm_kzalloc(&pdev->dev, sizeof(struct mxc_w1_device),
|
|
|
|
GFP_KERNEL);
|
2009-01-08 10:08:58 +08: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 07:15:24 +08:00
|
|
|
mdev->clk = devm_clk_get(&pdev->dev, NULL);
|
|
|
|
if (IS_ERR(mdev->clk))
|
|
|
|
return PTR_ERR(mdev->clk);
|
2009-01-08 10:08:58 +08:00
|
|
|
|
2018-05-02 16:55:31 +08:00
|
|
|
err = clk_prepare_enable(mdev->clk);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2013-11-29 19:39:29 +08: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 10:08:58 +08:00
|
|
|
|
2019-08-02 21:48:19 +08:00
|
|
|
mdev->regs = devm_platform_ioremap_resource(pdev, 0);
|
2018-05-02 16:55:31 +08:00
|
|
|
if (IS_ERR(mdev->regs)) {
|
|
|
|
err = PTR_ERR(mdev->regs);
|
|
|
|
goto out_disable_clk;
|
|
|
|
}
|
2013-11-29 19:39:30 +08:00
|
|
|
|
2014-05-08 15:56:39 +08: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 15:29:51 +08:00
|
|
|
writeb(clkdiv - 1, mdev->regs + MXC_W1_TIME_DIVIDER);
|
2009-01-08 10:08:58 +08: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 19:39:30 +08:00
|
|
|
platform_set_drvdata(pdev, mdev);
|
2009-01-08 10:08:58 +08:00
|
|
|
|
2013-11-29 19:39:30 +08:00
|
|
|
err = w1_add_master_device(&mdev->bus_master);
|
2009-01-08 10:08:58 +08:00
|
|
|
if (err)
|
2018-05-02 16:55:31 +08:00
|
|
|
goto out_disable_clk;
|
2009-01-08 10:08:58 +08:00
|
|
|
|
2018-05-02 16:55:31 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_disable_clk:
|
|
|
|
clk_disable_unprepare(mdev->clk);
|
2013-11-29 19:39:30 +08:00
|
|
|
return err;
|
2009-01-08 10:08:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* disassociate the w1 device from the driver
|
|
|
|
*/
|
2012-11-20 02:26:23 +08:00
|
|
|
static int mxc_w1_remove(struct platform_device *pdev)
|
2009-01-08 10:08:58 +08:00
|
|
|
{
|
|
|
|
struct mxc_w1_device *mdev = platform_get_drvdata(pdev);
|
|
|
|
|
|
|
|
w1_remove_master_device(&mdev->bus_master);
|
|
|
|
|
2012-03-08 03:59:36 +08:00
|
|
|
clk_disable_unprepare(mdev->clk);
|
2009-01-08 10:08:58 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-17 03:20:29 +08:00
|
|
|
static const struct of_device_id mxc_w1_dt_ids[] = {
|
2013-01-29 23:46:10 +08:00
|
|
|
{ .compatible = "fsl,imx21-owire" },
|
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, mxc_w1_dt_ids);
|
|
|
|
|
2009-01-08 10:08:58 +08:00
|
|
|
static struct platform_driver mxc_w1_driver = {
|
|
|
|
.driver = {
|
2013-01-29 23:46:10 +08:00
|
|
|
.name = "mxc_w1",
|
|
|
|
.of_match_table = mxc_w1_dt_ids,
|
2009-01-08 10:08:58 +08:00
|
|
|
},
|
|
|
|
.probe = mxc_w1_probe,
|
2012-12-22 04:55:26 +08:00
|
|
|
.remove = mxc_w1_remove,
|
2009-01-08 10:08:58 +08:00
|
|
|
};
|
2012-11-19 20:19:48 +08:00
|
|
|
module_platform_driver(mxc_w1_driver);
|
2009-01-08 10:08:58 +08:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Freescale Semiconductors Inc");
|
|
|
|
MODULE_DESCRIPTION("Driver for One-Wire on MXC");
|