blob: 3ef58c8dbf1143cb555a1be8cf121d4d456dd854 [file] [log] [blame]
Huang Shijie85bf6d42013-05-28 14:20:07 +08001/*
2 * EIM driver for Freescale's i.MX chips
3 *
4 * Copyright (C) 2013 Freescale Semiconductor, Inc.
5 *
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
9 */
10#include <linux/module.h>
11#include <linux/clk.h>
12#include <linux/io.h>
13#include <linux/of_device.h>
14
Alexander Shiyan3f98b6b2013-06-29 08:27:54 +040015struct imx_weim_devtype {
16 unsigned int cs_count;
17 unsigned int cs_regs_count;
18 unsigned int cs_stride;
19};
20
21static const struct imx_weim_devtype imx1_weim_devtype = {
22 .cs_count = 6,
23 .cs_regs_count = 2,
24 .cs_stride = 0x08,
25};
26
27static const struct imx_weim_devtype imx27_weim_devtype = {
28 .cs_count = 6,
29 .cs_regs_count = 3,
30 .cs_stride = 0x10,
31};
32
33static const struct imx_weim_devtype imx50_weim_devtype = {
34 .cs_count = 4,
35 .cs_regs_count = 6,
36 .cs_stride = 0x18,
37};
38
39static const struct imx_weim_devtype imx51_weim_devtype = {
40 .cs_count = 6,
41 .cs_regs_count = 6,
42 .cs_stride = 0x18,
43};
44
Huang Shijie85bf6d42013-05-28 14:20:07 +080045static const struct of_device_id weim_id_table[] = {
Alexander Shiyan3f98b6b2013-06-29 08:27:54 +040046 /* i.MX1/21 */
47 { .compatible = "fsl,imx1-weim", .data = &imx1_weim_devtype, },
48 /* i.MX25/27/31/35 */
49 { .compatible = "fsl,imx27-weim", .data = &imx27_weim_devtype, },
50 /* i.MX50/53/6Q */
51 { .compatible = "fsl,imx50-weim", .data = &imx50_weim_devtype, },
52 { .compatible = "fsl,imx6q-weim", .data = &imx50_weim_devtype, },
53 /* i.MX51 */
54 { .compatible = "fsl,imx51-weim", .data = &imx51_weim_devtype, },
55 { }
Huang Shijie85bf6d42013-05-28 14:20:07 +080056};
57MODULE_DEVICE_TABLE(of, weim_id_table);
58
Huang Shijie85bf6d42013-05-28 14:20:07 +080059/* Parse and set the timing for this device. */
Alexander Shiyan3f98b6b2013-06-29 08:27:54 +040060static int __init weim_timing_setup(struct device_node *np, void __iomem *base,
61 const struct imx_weim_devtype *devtype)
Huang Shijie85bf6d42013-05-28 14:20:07 +080062{
Alexander Shiyan3f98b6b2013-06-29 08:27:54 +040063 u32 cs_idx, value[devtype->cs_regs_count];
64 int i, ret;
Huang Shijie85bf6d42013-05-28 14:20:07 +080065
66 /* get the CS index from this child node's "reg" property. */
67 ret = of_property_read_u32(np, "reg", &cs_idx);
68 if (ret)
69 return ret;
70
Alexander Shiyan3f98b6b2013-06-29 08:27:54 +040071 if (cs_idx >= devtype->cs_count)
Huang Shijie85bf6d42013-05-28 14:20:07 +080072 return -EINVAL;
73
74 ret = of_property_read_u32_array(np, "fsl,weim-cs-timing",
Alexander Shiyan3f98b6b2013-06-29 08:27:54 +040075 value, devtype->cs_regs_count);
Huang Shijie85bf6d42013-05-28 14:20:07 +080076 if (ret)
77 return ret;
78
79 /* set the timing for WEIM */
Alexander Shiyan3f98b6b2013-06-29 08:27:54 +040080 for (i = 0; i < devtype->cs_regs_count; i++)
81 writel(value[i], base + cs_idx * devtype->cs_stride + i * 4);
82
Huang Shijie85bf6d42013-05-28 14:20:07 +080083 return 0;
84}
85
Alexander Shiyan29e54972013-06-29 08:27:52 +040086static int __init weim_parse_dt(struct platform_device *pdev,
87 void __iomem *base)
Huang Shijie85bf6d42013-05-28 14:20:07 +080088{
Alexander Shiyan3f98b6b2013-06-29 08:27:54 +040089 const struct of_device_id *of_id = of_match_device(weim_id_table,
90 &pdev->dev);
91 const struct imx_weim_devtype *devtype = of_id->data;
Huang Shijie85bf6d42013-05-28 14:20:07 +080092 struct device_node *child;
93 int ret;
94
95 for_each_child_of_node(pdev->dev.of_node, child) {
96 if (!child->name)
97 continue;
98
Alexander Shiyan3f98b6b2013-06-29 08:27:54 +040099 ret = weim_timing_setup(child, base, devtype);
Huang Shijie85bf6d42013-05-28 14:20:07 +0800100 if (ret) {
101 dev_err(&pdev->dev, "%s set timing failed.\n",
102 child->full_name);
103 return ret;
104 }
105 }
106
107 ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
108 if (ret)
109 dev_err(&pdev->dev, "%s fail to create devices.\n",
110 pdev->dev.of_node->full_name);
111 return ret;
112}
113
Alexander Shiyan29e54972013-06-29 08:27:52 +0400114static int __init weim_probe(struct platform_device *pdev)
Huang Shijie85bf6d42013-05-28 14:20:07 +0800115{
Huang Shijie85bf6d42013-05-28 14:20:07 +0800116 struct resource *res;
Alexander Shiyan70ac98d2013-06-29 08:27:50 +0400117 struct clk *clk;
118 void __iomem *base;
Alexander Shiyanb2d1fb72013-06-29 08:27:51 +0400119 int ret;
Huang Shijie85bf6d42013-05-28 14:20:07 +0800120
Huang Shijie85bf6d42013-05-28 14:20:07 +0800121 /* get the resource */
122 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Alexander Shiyan70ac98d2013-06-29 08:27:50 +0400123 base = devm_ioremap_resource(&pdev->dev, res);
Alexander Shiyanb2d1fb72013-06-29 08:27:51 +0400124 if (IS_ERR(base))
125 return PTR_ERR(base);
Huang Shijie85bf6d42013-05-28 14:20:07 +0800126
127 /* get the clock */
Alexander Shiyan70ac98d2013-06-29 08:27:50 +0400128 clk = devm_clk_get(&pdev->dev, NULL);
129 if (IS_ERR(clk))
Alexander Shiyanb2d1fb72013-06-29 08:27:51 +0400130 return PTR_ERR(clk);
Huang Shijie85bf6d42013-05-28 14:20:07 +0800131
Alexander Shiyan70ac98d2013-06-29 08:27:50 +0400132 ret = clk_prepare_enable(clk);
Huang Shijie85bf6d42013-05-28 14:20:07 +0800133 if (ret)
Alexander Shiyanb2d1fb72013-06-29 08:27:51 +0400134 return ret;
Huang Shijie85bf6d42013-05-28 14:20:07 +0800135
136 /* parse the device node */
Alexander Shiyan70ac98d2013-06-29 08:27:50 +0400137 ret = weim_parse_dt(pdev, base);
Alexander Shiyanb2d1fb72013-06-29 08:27:51 +0400138 if (ret)
Alexander Shiyan70ac98d2013-06-29 08:27:50 +0400139 clk_disable_unprepare(clk);
Alexander Shiyanb2d1fb72013-06-29 08:27:51 +0400140 else
141 dev_info(&pdev->dev, "Driver registered.\n");
Huang Shijie85bf6d42013-05-28 14:20:07 +0800142
Huang Shijie85bf6d42013-05-28 14:20:07 +0800143 return ret;
144}
145
146static struct platform_driver weim_driver = {
147 .driver = {
Alexander Shiyanfc608c72013-06-29 08:27:53 +0400148 .name = "imx-weim",
149 .owner = THIS_MODULE,
150 .of_match_table = weim_id_table,
Huang Shijie85bf6d42013-05-28 14:20:07 +0800151 },
Huang Shijie85bf6d42013-05-28 14:20:07 +0800152};
Alexander Shiyan29e54972013-06-29 08:27:52 +0400153module_platform_driver_probe(weim_driver, weim_probe);
Huang Shijie85bf6d42013-05-28 14:20:07 +0800154
Huang Shijie85bf6d42013-05-28 14:20:07 +0800155MODULE_AUTHOR("Freescale Semiconductor Inc.");
156MODULE_DESCRIPTION("i.MX EIM Controller Driver");
157MODULE_LICENSE("GPL");