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 | |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 15 | static const struct of_device_id weim_id_table[] = { |
| 16 | { .compatible = "fsl,imx6q-weim", }, |
| 17 | {} |
| 18 | }; |
| 19 | MODULE_DEVICE_TABLE(of, weim_id_table); |
| 20 | |
| 21 | #define CS_TIMING_LEN 6 |
| 22 | #define CS_REG_RANGE 0x18 |
| 23 | |
| 24 | /* Parse and set the timing for this device. */ |
Alexander Shiyan | 29e5497 | 2013-06-29 08:27:52 +0400 | [diff] [blame] | 25 | static int __init weim_timing_setup(struct device_node *np, void __iomem *base) |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 26 | { |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 27 | u32 value[CS_TIMING_LEN]; |
| 28 | u32 cs_idx; |
| 29 | int ret; |
| 30 | int i; |
| 31 | |
| 32 | /* get the CS index from this child node's "reg" property. */ |
| 33 | ret = of_property_read_u32(np, "reg", &cs_idx); |
| 34 | if (ret) |
| 35 | return ret; |
| 36 | |
| 37 | /* The weim has four chip selects. */ |
| 38 | if (cs_idx > 3) |
| 39 | return -EINVAL; |
| 40 | |
| 41 | ret = of_property_read_u32_array(np, "fsl,weim-cs-timing", |
| 42 | value, CS_TIMING_LEN); |
| 43 | if (ret) |
| 44 | return ret; |
| 45 | |
| 46 | /* set the timing for WEIM */ |
| 47 | for (i = 0; i < CS_TIMING_LEN; i++) |
Alexander Shiyan | 70ac98d | 2013-06-29 08:27:50 +0400 | [diff] [blame] | 48 | writel(value[i], base + cs_idx * CS_REG_RANGE + i * 4); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 49 | return 0; |
| 50 | } |
| 51 | |
Alexander Shiyan | 29e5497 | 2013-06-29 08:27:52 +0400 | [diff] [blame] | 52 | static int __init weim_parse_dt(struct platform_device *pdev, |
| 53 | void __iomem *base) |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 54 | { |
| 55 | struct device_node *child; |
| 56 | int ret; |
| 57 | |
| 58 | for_each_child_of_node(pdev->dev.of_node, child) { |
| 59 | if (!child->name) |
| 60 | continue; |
| 61 | |
Alexander Shiyan | 70ac98d | 2013-06-29 08:27:50 +0400 | [diff] [blame] | 62 | ret = weim_timing_setup(child, base); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 63 | if (ret) { |
| 64 | dev_err(&pdev->dev, "%s set timing failed.\n", |
| 65 | child->full_name); |
| 66 | return ret; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev); |
| 71 | if (ret) |
| 72 | dev_err(&pdev->dev, "%s fail to create devices.\n", |
| 73 | pdev->dev.of_node->full_name); |
| 74 | return ret; |
| 75 | } |
| 76 | |
Alexander Shiyan | 29e5497 | 2013-06-29 08:27:52 +0400 | [diff] [blame] | 77 | static int __init weim_probe(struct platform_device *pdev) |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 78 | { |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 79 | struct resource *res; |
Alexander Shiyan | 70ac98d | 2013-06-29 08:27:50 +0400 | [diff] [blame] | 80 | struct clk *clk; |
| 81 | void __iomem *base; |
Alexander Shiyan | b2d1fb7 | 2013-06-29 08:27:51 +0400 | [diff] [blame] | 82 | int ret; |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 83 | |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 84 | /* get the resource */ |
| 85 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Alexander Shiyan | 70ac98d | 2013-06-29 08:27:50 +0400 | [diff] [blame] | 86 | base = devm_ioremap_resource(&pdev->dev, res); |
Alexander Shiyan | b2d1fb7 | 2013-06-29 08:27:51 +0400 | [diff] [blame] | 87 | if (IS_ERR(base)) |
| 88 | return PTR_ERR(base); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 89 | |
| 90 | /* get the clock */ |
Alexander Shiyan | 70ac98d | 2013-06-29 08:27:50 +0400 | [diff] [blame] | 91 | clk = devm_clk_get(&pdev->dev, NULL); |
| 92 | if (IS_ERR(clk)) |
Alexander Shiyan | b2d1fb7 | 2013-06-29 08:27:51 +0400 | [diff] [blame] | 93 | return PTR_ERR(clk); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 94 | |
Alexander Shiyan | 70ac98d | 2013-06-29 08:27:50 +0400 | [diff] [blame] | 95 | ret = clk_prepare_enable(clk); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 96 | if (ret) |
Alexander Shiyan | b2d1fb7 | 2013-06-29 08:27:51 +0400 | [diff] [blame] | 97 | return ret; |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 98 | |
| 99 | /* parse the device node */ |
Alexander Shiyan | 70ac98d | 2013-06-29 08:27:50 +0400 | [diff] [blame] | 100 | ret = weim_parse_dt(pdev, base); |
Alexander Shiyan | b2d1fb7 | 2013-06-29 08:27:51 +0400 | [diff] [blame] | 101 | if (ret) |
Alexander Shiyan | 70ac98d | 2013-06-29 08:27:50 +0400 | [diff] [blame] | 102 | clk_disable_unprepare(clk); |
Alexander Shiyan | b2d1fb7 | 2013-06-29 08:27:51 +0400 | [diff] [blame] | 103 | else |
| 104 | dev_info(&pdev->dev, "Driver registered.\n"); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 105 | |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | static struct platform_driver weim_driver = { |
| 110 | .driver = { |
Alexander Shiyan | fc608c7 | 2013-06-29 08:27:53 +0400 | [diff] [blame^] | 111 | .name = "imx-weim", |
| 112 | .owner = THIS_MODULE, |
| 113 | .of_match_table = weim_id_table, |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 114 | }, |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 115 | }; |
Alexander Shiyan | 29e5497 | 2013-06-29 08:27:52 +0400 | [diff] [blame] | 116 | module_platform_driver_probe(weim_driver, weim_probe); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 117 | |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 118 | MODULE_AUTHOR("Freescale Semiconductor Inc."); |
| 119 | MODULE_DESCRIPTION("i.MX EIM Controller Driver"); |
| 120 | MODULE_LICENSE("GPL"); |