Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 1 | /* |
| 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 Shiyan | 3f98b6b | 2013-06-29 08:27:54 +0400 | [diff] [blame^] | 15 | struct imx_weim_devtype { |
| 16 | unsigned int cs_count; |
| 17 | unsigned int cs_regs_count; |
| 18 | unsigned int cs_stride; |
| 19 | }; |
| 20 | |
| 21 | static const struct imx_weim_devtype imx1_weim_devtype = { |
| 22 | .cs_count = 6, |
| 23 | .cs_regs_count = 2, |
| 24 | .cs_stride = 0x08, |
| 25 | }; |
| 26 | |
| 27 | static const struct imx_weim_devtype imx27_weim_devtype = { |
| 28 | .cs_count = 6, |
| 29 | .cs_regs_count = 3, |
| 30 | .cs_stride = 0x10, |
| 31 | }; |
| 32 | |
| 33 | static const struct imx_weim_devtype imx50_weim_devtype = { |
| 34 | .cs_count = 4, |
| 35 | .cs_regs_count = 6, |
| 36 | .cs_stride = 0x18, |
| 37 | }; |
| 38 | |
| 39 | static const struct imx_weim_devtype imx51_weim_devtype = { |
| 40 | .cs_count = 6, |
| 41 | .cs_regs_count = 6, |
| 42 | .cs_stride = 0x18, |
| 43 | }; |
| 44 | |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 45 | static const struct of_device_id weim_id_table[] = { |
Alexander Shiyan | 3f98b6b | 2013-06-29 08:27:54 +0400 | [diff] [blame^] | 46 | /* 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 Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 56 | }; |
| 57 | MODULE_DEVICE_TABLE(of, weim_id_table); |
| 58 | |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 59 | /* Parse and set the timing for this device. */ |
Alexander Shiyan | 3f98b6b | 2013-06-29 08:27:54 +0400 | [diff] [blame^] | 60 | static int __init weim_timing_setup(struct device_node *np, void __iomem *base, |
| 61 | const struct imx_weim_devtype *devtype) |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 62 | { |
Alexander Shiyan | 3f98b6b | 2013-06-29 08:27:54 +0400 | [diff] [blame^] | 63 | u32 cs_idx, value[devtype->cs_regs_count]; |
| 64 | int i, ret; |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 65 | |
| 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 Shiyan | 3f98b6b | 2013-06-29 08:27:54 +0400 | [diff] [blame^] | 71 | if (cs_idx >= devtype->cs_count) |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 72 | return -EINVAL; |
| 73 | |
| 74 | ret = of_property_read_u32_array(np, "fsl,weim-cs-timing", |
Alexander Shiyan | 3f98b6b | 2013-06-29 08:27:54 +0400 | [diff] [blame^] | 75 | value, devtype->cs_regs_count); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 76 | if (ret) |
| 77 | return ret; |
| 78 | |
| 79 | /* set the timing for WEIM */ |
Alexander Shiyan | 3f98b6b | 2013-06-29 08:27:54 +0400 | [diff] [blame^] | 80 | for (i = 0; i < devtype->cs_regs_count; i++) |
| 81 | writel(value[i], base + cs_idx * devtype->cs_stride + i * 4); |
| 82 | |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
Alexander Shiyan | 29e5497 | 2013-06-29 08:27:52 +0400 | [diff] [blame] | 86 | static int __init weim_parse_dt(struct platform_device *pdev, |
| 87 | void __iomem *base) |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 88 | { |
Alexander Shiyan | 3f98b6b | 2013-06-29 08:27:54 +0400 | [diff] [blame^] | 89 | 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 Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 92 | 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 Shiyan | 3f98b6b | 2013-06-29 08:27:54 +0400 | [diff] [blame^] | 99 | ret = weim_timing_setup(child, base, devtype); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 100 | 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 Shiyan | 29e5497 | 2013-06-29 08:27:52 +0400 | [diff] [blame] | 114 | static int __init weim_probe(struct platform_device *pdev) |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 115 | { |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 116 | struct resource *res; |
Alexander Shiyan | 70ac98d | 2013-06-29 08:27:50 +0400 | [diff] [blame] | 117 | struct clk *clk; |
| 118 | void __iomem *base; |
Alexander Shiyan | b2d1fb7 | 2013-06-29 08:27:51 +0400 | [diff] [blame] | 119 | int ret; |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 120 | |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 121 | /* get the resource */ |
| 122 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Alexander Shiyan | 70ac98d | 2013-06-29 08:27:50 +0400 | [diff] [blame] | 123 | base = devm_ioremap_resource(&pdev->dev, res); |
Alexander Shiyan | b2d1fb7 | 2013-06-29 08:27:51 +0400 | [diff] [blame] | 124 | if (IS_ERR(base)) |
| 125 | return PTR_ERR(base); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 126 | |
| 127 | /* get the clock */ |
Alexander Shiyan | 70ac98d | 2013-06-29 08:27:50 +0400 | [diff] [blame] | 128 | clk = devm_clk_get(&pdev->dev, NULL); |
| 129 | if (IS_ERR(clk)) |
Alexander Shiyan | b2d1fb7 | 2013-06-29 08:27:51 +0400 | [diff] [blame] | 130 | return PTR_ERR(clk); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 131 | |
Alexander Shiyan | 70ac98d | 2013-06-29 08:27:50 +0400 | [diff] [blame] | 132 | ret = clk_prepare_enable(clk); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 133 | if (ret) |
Alexander Shiyan | b2d1fb7 | 2013-06-29 08:27:51 +0400 | [diff] [blame] | 134 | return ret; |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 135 | |
| 136 | /* parse the device node */ |
Alexander Shiyan | 70ac98d | 2013-06-29 08:27:50 +0400 | [diff] [blame] | 137 | ret = weim_parse_dt(pdev, base); |
Alexander Shiyan | b2d1fb7 | 2013-06-29 08:27:51 +0400 | [diff] [blame] | 138 | if (ret) |
Alexander Shiyan | 70ac98d | 2013-06-29 08:27:50 +0400 | [diff] [blame] | 139 | clk_disable_unprepare(clk); |
Alexander Shiyan | b2d1fb7 | 2013-06-29 08:27:51 +0400 | [diff] [blame] | 140 | else |
| 141 | dev_info(&pdev->dev, "Driver registered.\n"); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 142 | |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | static struct platform_driver weim_driver = { |
| 147 | .driver = { |
Alexander Shiyan | fc608c7 | 2013-06-29 08:27:53 +0400 | [diff] [blame] | 148 | .name = "imx-weim", |
| 149 | .owner = THIS_MODULE, |
| 150 | .of_match_table = weim_id_table, |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 151 | }, |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 152 | }; |
Alexander Shiyan | 29e5497 | 2013-06-29 08:27:52 +0400 | [diff] [blame] | 153 | module_platform_driver_probe(weim_driver, weim_probe); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 154 | |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 155 | MODULE_AUTHOR("Freescale Semiconductor Inc."); |
| 156 | MODULE_DESCRIPTION("i.MX EIM Controller Driver"); |
| 157 | MODULE_LICENSE("GPL"); |