blob: dacb5919970c5346492afc90e21bd8621737ecab [file] [log] [blame]
Sascha Hauera5fd9132009-01-07 18:08:58 -08001/*
2 * Copyright 2005-2008 Freescale Semiconductor, Inc. All Rights Reserved.
3 * Copyright 2008 Luotao Fu, kernel@pengutronix.de
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Sascha Hauera5fd9132009-01-07 18:08:58 -080013 */
14
Sascha Hauera5fd9132009-01-07 18:08:58 -080015#include <linux/clk.h>
16#include <linux/delay.h>
17#include <linux/io.h>
Alexander Shiyanb0dceb62014-05-08 11:56:38 +040018#include <linux/jiffies.h>
Alexander Shiyan18fd9e3592014-02-22 11:29:50 +040019#include <linux/module.h>
20#include <linux/platform_device.h>
Sascha Hauera5fd9132009-01-07 18:08:58 -080021
22#include "../w1.h"
23#include "../w1_int.h"
Sascha Hauera5fd9132009-01-07 18:08:58 -080024
Sascha Hauera5fd9132009-01-07 18:08:58 -080025/*
26 * MXC W1 Register offsets
27 */
Alexander Shiyan18fd9e3592014-02-22 11:29:50 +040028#define MXC_W1_CONTROL 0x00
29# define MXC_W1_CONTROL_RDST BIT(3)
30# define MXC_W1_CONTROL_WR(x) BIT(5 - (x))
31# define MXC_W1_CONTROL_PST BIT(6)
32# define MXC_W1_CONTROL_RPP BIT(7)
33#define MXC_W1_TIME_DIVIDER 0x02
34#define MXC_W1_RESET 0x04
Alexander Shiyanb7ce0b52014-05-08 11:56:39 +040035# define MXC_W1_RESET_RST BIT(0)
Sascha Hauera5fd9132009-01-07 18:08:58 -080036
37struct mxc_w1_device {
38 void __iomem *regs;
Sascha Hauera5fd9132009-01-07 18:08:58 -080039 struct clk *clk;
40 struct w1_bus_master bus_master;
41};
42
43/*
44 * this is the low level routine to
45 * reset the device on the One Wire interface
46 * on the hardware
47 */
48static u8 mxc_w1_ds2_reset_bus(void *data)
49{
Sascha Hauera5fd9132009-01-07 18:08:58 -080050 struct mxc_w1_device *dev = data;
Alexander Shiyanb0dceb62014-05-08 11:56:38 +040051 unsigned long timeout;
Sascha Hauera5fd9132009-01-07 18:08:58 -080052
Alexander Shiyanb0dceb62014-05-08 11:56:38 +040053 writeb(MXC_W1_CONTROL_RPP, dev->regs + MXC_W1_CONTROL);
Sascha Hauera5fd9132009-01-07 18:08:58 -080054
Alexander Shiyanb0dceb62014-05-08 11:56:38 +040055 /* Wait for reset sequence 511+512us, use 1500us for sure */
56 timeout = jiffies + usecs_to_jiffies(1500);
Sascha Hauera5fd9132009-01-07 18:08:58 -080057
Alexander Shiyanb0dceb62014-05-08 11:56:38 +040058 udelay(511 + 512);
Sascha Hauera5fd9132009-01-07 18:08:58 -080059
Alexander Shiyanb0dceb62014-05-08 11:56:38 +040060 do {
61 u8 ctrl = readb(dev->regs + MXC_W1_CONTROL);
62
63 /* PST bit is valid after the RPP bit is self-cleared */
64 if (!(ctrl & MXC_W1_CONTROL_RPP))
65 return !(ctrl & MXC_W1_CONTROL_PST);
66 } while (time_is_after_jiffies(timeout));
67
68 return 1;
Sascha Hauera5fd9132009-01-07 18:08:58 -080069}
70
71/*
72 * this is the low level routine to read/write a bit on the One Wire
73 * interface on the hardware. It does write 0 if parameter bit is set
74 * to 0, otherwise a write 1/read.
75 */
76static u8 mxc_w1_ds2_touch_bit(void *data, u8 bit)
77{
Alexander Shiyanf80b2582014-05-08 11:56:40 +040078 struct mxc_w1_device *dev = data;
79 unsigned long timeout;
Sascha Hauera5fd9132009-01-07 18:08:58 -080080
Alexander Shiyanf80b2582014-05-08 11:56:40 +040081 writeb(MXC_W1_CONTROL_WR(bit), dev->regs + MXC_W1_CONTROL);
Sascha Hauera5fd9132009-01-07 18:08:58 -080082
Alexander Shiyanf80b2582014-05-08 11:56:40 +040083 /* Wait for read/write bit (60us, Max 120us), use 200us for sure */
84 timeout = jiffies + usecs_to_jiffies(200);
Sascha Hauera5fd9132009-01-07 18:08:58 -080085
Alexander Shiyanf80b2582014-05-08 11:56:40 +040086 udelay(60);
Sascha Hauera5fd9132009-01-07 18:08:58 -080087
Alexander Shiyanf80b2582014-05-08 11:56:40 +040088 do {
89 u8 ctrl = readb(dev->regs + MXC_W1_CONTROL);
90
91 /* RDST bit is valid after the WR1/RD bit is self-cleared */
92 if (!(ctrl & MXC_W1_CONTROL_WR(bit)))
93 return !!(ctrl & MXC_W1_CONTROL_RDST);
94 } while (time_is_after_jiffies(timeout));
95
96 return 0;
Sascha Hauera5fd9132009-01-07 18:08:58 -080097}
98
Bill Pemberton479e2bc2012-11-19 13:21:43 -050099static int mxc_w1_probe(struct platform_device *pdev)
Sascha Hauera5fd9132009-01-07 18:08:58 -0800100{
101 struct mxc_w1_device *mdev;
Alexander Shiyan71531f52013-11-29 15:39:29 +0400102 unsigned long clkrate;
Sascha Hauera5fd9132009-01-07 18:08:58 -0800103 struct resource *res;
Alexander Shiyana0822632013-11-29 15:39:28 +0400104 unsigned int clkdiv;
Alexander Shiyan001d1952013-11-29 15:39:30 +0400105 int err;
Sascha Hauera5fd9132009-01-07 18:08:58 -0800106
Julia Lawalle5279ff2012-12-07 00:15:24 +0100107 mdev = devm_kzalloc(&pdev->dev, sizeof(struct mxc_w1_device),
108 GFP_KERNEL);
Sascha Hauera5fd9132009-01-07 18:08:58 -0800109 if (!mdev)
110 return -ENOMEM;
111
Julia Lawalle5279ff2012-12-07 00:15:24 +0100112 mdev->clk = devm_clk_get(&pdev->dev, NULL);
113 if (IS_ERR(mdev->clk))
114 return PTR_ERR(mdev->clk);
Sascha Hauera5fd9132009-01-07 18:08:58 -0800115
Stefan Potyraaec3dd52018-05-02 10:55:31 +0200116 err = clk_prepare_enable(mdev->clk);
117 if (err)
118 return err;
119
Alexander Shiyan71531f52013-11-29 15:39:29 +0400120 clkrate = clk_get_rate(mdev->clk);
121 if (clkrate < 10000000)
122 dev_warn(&pdev->dev,
123 "Low clock frequency causes improper function\n");
124
125 clkdiv = DIV_ROUND_CLOSEST(clkrate, 1000000);
126 clkrate /= clkdiv;
127 if ((clkrate < 980000) || (clkrate > 1020000))
128 dev_warn(&pdev->dev,
129 "Incorrect time base frequency %lu Hz\n", clkrate);
Sascha Hauera5fd9132009-01-07 18:08:58 -0800130
Julia Lawalle5279ff2012-12-07 00:15:24 +0100131 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Fabio Estevam388f7bd2013-03-11 20:18:15 -0300132 mdev->regs = devm_ioremap_resource(&pdev->dev, res);
Stefan Potyraaec3dd52018-05-02 10:55:31 +0200133 if (IS_ERR(mdev->regs)) {
134 err = PTR_ERR(mdev->regs);
135 goto out_disable_clk;
136 }
Alexander Shiyan001d1952013-11-29 15:39:30 +0400137
Alexander Shiyanb7ce0b52014-05-08 11:56:39 +0400138 /* Software reset 1-Wire module */
139 writeb(MXC_W1_RESET_RST, mdev->regs + MXC_W1_RESET);
140 writeb(0, mdev->regs + MXC_W1_RESET);
141
Alexander Shiyanfc945d62014-02-22 11:29:51 +0400142 writeb(clkdiv - 1, mdev->regs + MXC_W1_TIME_DIVIDER);
Sascha Hauera5fd9132009-01-07 18:08:58 -0800143
144 mdev->bus_master.data = mdev;
145 mdev->bus_master.reset_bus = mxc_w1_ds2_reset_bus;
146 mdev->bus_master.touch_bit = mxc_w1_ds2_touch_bit;
147
Sascha Hauera5fd9132009-01-07 18:08:58 -0800148 platform_set_drvdata(pdev, mdev);
Alexander Shiyan001d1952013-11-29 15:39:30 +0400149
150 err = w1_add_master_device(&mdev->bus_master);
151 if (err)
Stefan Potyraaec3dd52018-05-02 10:55:31 +0200152 goto out_disable_clk;
Alexander Shiyan001d1952013-11-29 15:39:30 +0400153
Stefan Potyraaec3dd52018-05-02 10:55:31 +0200154 return 0;
155
156out_disable_clk:
157 clk_disable_unprepare(mdev->clk);
Alexander Shiyan001d1952013-11-29 15:39:30 +0400158 return err;
Sascha Hauera5fd9132009-01-07 18:08:58 -0800159}
160
161/*
162 * disassociate the w1 device from the driver
163 */
Bill Pemberton82849a92012-11-19 13:26:23 -0500164static int mxc_w1_remove(struct platform_device *pdev)
Sascha Hauera5fd9132009-01-07 18:08:58 -0800165{
166 struct mxc_w1_device *mdev = platform_get_drvdata(pdev);
Sascha Hauera5fd9132009-01-07 18:08:58 -0800167
168 w1_remove_master_device(&mdev->bus_master);
169
Sascha Hauer60178b62012-03-07 20:59:36 +0100170 clk_disable_unprepare(mdev->clk);
Sascha Hauera5fd9132009-01-07 18:08:58 -0800171
Sascha Hauera5fd9132009-01-07 18:08:58 -0800172 return 0;
173}
174
Fabian Frederick0a56c0e2015-03-16 20:20:29 +0100175static const struct of_device_id mxc_w1_dt_ids[] = {
Martin Fuzzey28c55dc2013-01-29 16:46:10 +0100176 { .compatible = "fsl,imx21-owire" },
177 { /* sentinel */ }
178};
179MODULE_DEVICE_TABLE(of, mxc_w1_dt_ids);
180
Sascha Hauera5fd9132009-01-07 18:08:58 -0800181static struct platform_driver mxc_w1_driver = {
182 .driver = {
Martin Fuzzey28c55dc2013-01-29 16:46:10 +0100183 .name = "mxc_w1",
184 .of_match_table = mxc_w1_dt_ids,
Sascha Hauera5fd9132009-01-07 18:08:58 -0800185 },
186 .probe = mxc_w1_probe,
Greg Kroah-Hartman10532fe2012-12-21 12:55:26 -0800187 .remove = mxc_w1_remove,
Sascha Hauera5fd9132009-01-07 18:08:58 -0800188};
Fabio Estevamfd21bfc2012-11-19 10:19:48 -0200189module_platform_driver(mxc_w1_driver);
Sascha Hauera5fd9132009-01-07 18:08:58 -0800190
191MODULE_LICENSE("GPL");
192MODULE_AUTHOR("Freescale Semiconductors Inc");
193MODULE_DESCRIPTION("Driver for One-Wire on MXC");