xref: /openbmc/linux/drivers/mfd/atmel-flexcom.c (revision 6d99a79c)
1 /*
2  * Driver for Atmel Flexcom
3  *
4  * Copyright (C) 2015 Atmel Corporation
5  *
6  * Author: Cyrille Pitchen <cyrille.pitchen@atmel.com>
7  *
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.
11  *
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.
16  *
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/>.
19  */
20 
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>
31 
32 /* I/O register offsets */
33 #define FLEX_MR		0x0	/* Mode Register */
34 #define FLEX_VERSION	0xfc	/* Version Register */
35 
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)
41 
42 struct atmel_flexcom {
43 	void __iomem *base;
44 	u32 opmode;
45 	struct clk *clk;
46 };
47 
48 static int atmel_flexcom_probe(struct platform_device *pdev)
49 {
50 	struct device_node *np = pdev->dev.of_node;
51 	struct resource *res;
52 	struct atmel_flexcom *ddata;
53 	int err;
54 
55 	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
56 	if (!ddata)
57 		return -ENOMEM;
58 
59 	platform_set_drvdata(pdev, ddata);
60 
61 	err = of_property_read_u32(np, "atmel,flexcom-mode", &ddata->opmode);
62 	if (err)
63 		return err;
64 
65 	if (ddata->opmode < ATMEL_FLEXCOM_MODE_USART ||
66 	    ddata->opmode > ATMEL_FLEXCOM_MODE_TWI)
67 		return -EINVAL;
68 
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);
73 
74 	ddata->clk = devm_clk_get(&pdev->dev, NULL);
75 	if (IS_ERR(ddata->clk))
76 		return PTR_ERR(ddata->clk);
77 
78 	err = clk_prepare_enable(ddata->clk);
79 	if (err)
80 		return err;
81 
82 	/*
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.
87 	 */
88 	writel(FLEX_MR_OPMODE(ddata->opmode), ddata->base + FLEX_MR);
89 
90 	clk_disable_unprepare(ddata->clk);
91 
92 	return devm_of_platform_populate(&pdev->dev);
93 }
94 
95 static const struct of_device_id atmel_flexcom_of_match[] = {
96 	{ .compatible = "atmel,sama5d2-flexcom" },
97 	{ /* sentinel */ }
98 };
99 MODULE_DEVICE_TABLE(of, atmel_flexcom_of_match);
100 
101 #ifdef CONFIG_PM_SLEEP
102 static int atmel_flexcom_resume(struct device *dev)
103 {
104 	struct atmel_flexcom *ddata = dev_get_drvdata(dev);
105 	int err;
106 	u32 val;
107 
108 	err = clk_prepare_enable(ddata->clk);
109 	if (err)
110 		return err;
111 
112 	val = FLEX_MR_OPMODE(ddata->opmode),
113 	writel(val, ddata->base + FLEX_MR);
114 
115 	clk_disable_unprepare(ddata->clk);
116 
117 	return 0;
118 }
119 #endif
120 
121 static SIMPLE_DEV_PM_OPS(atmel_flexcom_pm_ops, NULL,
122 			 atmel_flexcom_resume);
123 
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,
130 	},
131 };
132 
133 module_platform_driver(atmel_flexcom_driver);
134 
135 MODULE_AUTHOR("Cyrille Pitchen <cyrille.pitchen@atmel.com>");
136 MODULE_DESCRIPTION("Atmel Flexcom MFD driver");
137 MODULE_LICENSE("GPL v2");
138