Linux 4.19.133
[linux/fpc-iii.git] / drivers / mfd / atmel-flexcom.c
blobf684a93a334082d40c1fa1d2f28ad9ff19d8e96a
1 /*
2 * Driver for Atmel Flexcom
4 * Copyright (C) 2015 Atmel Corporation
6 * Author: Cyrille Pitchen <cyrille.pitchen@atmel.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/platform_device.h>
25 #include <linux/of.h>
26 #include <linux/of_platform.h>
27 #include <linux/err.h>
28 #include <linux/io.h>
29 #include <linux/clk.h>
30 #include <dt-bindings/mfd/atmel-flexcom.h>
32 /* I/O register offsets */
33 #define FLEX_MR 0x0 /* Mode Register */
34 #define FLEX_VERSION 0xfc /* Version Register */
36 /* Mode Register bit fields */
37 #define FLEX_MR_OPMODE_OFFSET (0) /* Operating Mode */
38 #define FLEX_MR_OPMODE_MASK (0x3 << FLEX_MR_OPMODE_OFFSET)
39 #define FLEX_MR_OPMODE(opmode) (((opmode) << FLEX_MR_OPMODE_OFFSET) & \
40 FLEX_MR_OPMODE_MASK)
42 struct atmel_flexcom {
43 void __iomem *base;
44 u32 opmode;
45 struct clk *clk;
48 static int atmel_flexcom_probe(struct platform_device *pdev)
50 struct device_node *np = pdev->dev.of_node;
51 struct resource *res;
52 struct atmel_flexcom *ddata;
53 int err;
55 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
56 if (!ddata)
57 return -ENOMEM;
59 platform_set_drvdata(pdev, ddata);
61 err = of_property_read_u32(np, "atmel,flexcom-mode", &ddata->opmode);
62 if (err)
63 return err;
65 if (ddata->opmode < ATMEL_FLEXCOM_MODE_USART ||
66 ddata->opmode > ATMEL_FLEXCOM_MODE_TWI)
67 return -EINVAL;
69 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
70 ddata->base = devm_ioremap_resource(&pdev->dev, res);
71 if (IS_ERR(ddata->base))
72 return PTR_ERR(ddata->base);
74 ddata->clk = devm_clk_get(&pdev->dev, NULL);
75 if (IS_ERR(ddata->clk))
76 return PTR_ERR(ddata->clk);
78 err = clk_prepare_enable(ddata->clk);
79 if (err)
80 return err;
83 * Set the Operating Mode in the Mode Register: only the selected device
84 * is clocked. Hence, registers of the other serial devices remain
85 * inaccessible and are read as zero. Also the external I/O lines of the
86 * Flexcom are muxed to reach the selected device.
88 writel(FLEX_MR_OPMODE(ddata->opmode), ddata->base + FLEX_MR);
90 clk_disable_unprepare(ddata->clk);
92 return devm_of_platform_populate(&pdev->dev);
95 static const struct of_device_id atmel_flexcom_of_match[] = {
96 { .compatible = "atmel,sama5d2-flexcom" },
97 { /* sentinel */ }
99 MODULE_DEVICE_TABLE(of, atmel_flexcom_of_match);
101 #ifdef CONFIG_PM_SLEEP
102 static int atmel_flexcom_resume(struct device *dev)
104 struct atmel_flexcom *ddata = dev_get_drvdata(dev);
105 int err;
106 u32 val;
108 err = clk_prepare_enable(ddata->clk);
109 if (err)
110 return err;
112 val = FLEX_MR_OPMODE(ddata->opmode),
113 writel(val, ddata->base + FLEX_MR);
115 clk_disable_unprepare(ddata->clk);
117 return 0;
119 #endif
121 static SIMPLE_DEV_PM_OPS(atmel_flexcom_pm_ops, NULL,
122 atmel_flexcom_resume);
124 static struct platform_driver atmel_flexcom_driver = {
125 .probe = atmel_flexcom_probe,
126 .driver = {
127 .name = "atmel_flexcom",
128 .pm = &atmel_flexcom_pm_ops,
129 .of_match_table = atmel_flexcom_of_match,
133 module_platform_driver(atmel_flexcom_driver);
135 MODULE_AUTHOR("Cyrille Pitchen <cyrille.pitchen@atmel.com>");
136 MODULE_DESCRIPTION("Atmel Flexcom MFD driver");
137 MODULE_LICENSE("GPL v2");