2009-01-08 10:08:58 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2005-2008 Freescale Semiconductor, Inc. All Rights Reserved.
|
|
|
|
* Copyright 2008 Luotao Fu, kernel@pengutronix.de
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/io.h>
|
2014-02-22 15:29:50 +08:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/platform_device.h>
|
2009-01-08 10:08:58 +08:00
|
|
|
|
|
|
|
#include "../w1.h"
|
|
|
|
#include "../w1_int.h"
|
|
|
|
|
|
|
|
/* According to the mx27 Datasheet the reset procedure should take up to about
|
|
|
|
* 1350us. We set the timeout to 500*100us = 50ms for sure */
|
|
|
|
#define MXC_W1_RESET_TIMEOUT 500
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
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)
|
|
|
|
{
|
|
|
|
u8 reg_val;
|
|
|
|
unsigned int timeout_cnt = 0;
|
|
|
|
struct mxc_w1_device *dev = data;
|
|
|
|
|
2014-02-22 15:29:51 +08:00
|
|
|
writeb(MXC_W1_CONTROL_RPP, (dev->regs + MXC_W1_CONTROL));
|
2009-01-08 10:08:58 +08:00
|
|
|
|
|
|
|
while (1) {
|
2014-02-22 15:29:51 +08:00
|
|
|
reg_val = readb(dev->regs + MXC_W1_CONTROL);
|
2009-01-08 10:08:58 +08:00
|
|
|
|
2014-02-22 15:29:50 +08:00
|
|
|
if (!(reg_val & MXC_W1_CONTROL_RPP) ||
|
2009-01-08 10:08:58 +08:00
|
|
|
timeout_cnt > MXC_W1_RESET_TIMEOUT)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
timeout_cnt++;
|
|
|
|
|
|
|
|
udelay(100);
|
|
|
|
}
|
2014-02-22 15:29:50 +08:00
|
|
|
return !!(reg_val & MXC_W1_CONTROL_PST);
|
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)
|
|
|
|
{
|
|
|
|
struct mxc_w1_device *mdev = data;
|
|
|
|
void __iomem *ctrl_addr = mdev->regs + MXC_W1_CONTROL;
|
|
|
|
unsigned int timeout_cnt = 400; /* Takes max. 120us according to
|
|
|
|
* datasheet.
|
|
|
|
*/
|
|
|
|
|
2014-02-22 15:29:51 +08:00
|
|
|
writeb(MXC_W1_CONTROL_WR(bit), ctrl_addr);
|
2009-01-08 10:08:58 +08:00
|
|
|
|
|
|
|
while (timeout_cnt--) {
|
2014-02-22 15:29:51 +08:00
|
|
|
if (!(readb(ctrl_addr) & MXC_W1_CONTROL_WR(bit)))
|
2009-01-08 10:08:58 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
udelay(1);
|
|
|
|
}
|
|
|
|
|
2014-02-22 15:29:51 +08:00
|
|
|
return !!(readb(ctrl_addr) & MXC_W1_CONTROL_RDST);
|
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;
|
2009-01-08 10:08:58 +08:00
|
|
|
struct resource *res;
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
2013-03-12 07:18:15 +08:00
|
|
|
mdev->regs = devm_ioremap_resource(&pdev->dev, res);
|
|
|
|
if (IS_ERR(mdev->regs))
|
|
|
|
return PTR_ERR(mdev->regs);
|
2009-01-08 10:08:58 +08:00
|
|
|
|
2013-11-29 19:39:30 +08:00
|
|
|
err = clk_prepare_enable(mdev->clk);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
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)
|
2013-11-29 19:39:30 +08:00
|
|
|
clk_disable_unprepare(mdev->clk);
|
2009-01-08 10:08:58 +08:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-01-29 23:46:10 +08:00
|
|
|
static struct of_device_id mxc_w1_dt_ids[] = {
|
|
|
|
{ .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",
|
2014-02-22 15:29:50 +08:00
|
|
|
.owner = THIS_MODULE,
|
2013-01-29 23:46:10 +08:00
|
|
|
.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");
|