Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Driver for Atmel Flexcom
4 *
5 * Copyright (C) 2015 Atmel Corporation
6 *
7 * Author: Cyrille Pitchen <cyrille.pitchen@atmel.com>
8 */
9
10#include <linux/module.h>
11#include <linux/types.h>
12#include <linux/kernel.h>
13#include <linux/platform_device.h>
14#include <linux/of.h>
15#include <linux/of_platform.h>
16#include <linux/err.h>
17#include <linux/io.h>
18#include <linux/clk.h>
19#include <dt-bindings/mfd/atmel-flexcom.h>
20
21/* I/O register offsets */
22#define FLEX_MR 0x0 /* Mode Register */
23#define FLEX_VERSION 0xfc /* Version Register */
24
25/* Mode Register bit fields */
26#define FLEX_MR_OPMODE_OFFSET (0) /* Operating Mode */
27#define FLEX_MR_OPMODE_MASK (0x3 << FLEX_MR_OPMODE_OFFSET)
28#define FLEX_MR_OPMODE(opmode) (((opmode) << FLEX_MR_OPMODE_OFFSET) & \
29 FLEX_MR_OPMODE_MASK)
30
31struct atmel_flexcom {
32 void __iomem *base;
33 u32 opmode;
34 struct clk *clk;
35};
36
37static int atmel_flexcom_probe(struct platform_device *pdev)
38{
39 struct device_node *np = pdev->dev.of_node;
40 struct atmel_flexcom *ddata;
41 int err;
42
43 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
44 if (!ddata)
45 return -ENOMEM;
46
47 platform_set_drvdata(pdev, ddata);
48
49 err = of_property_read_u32(np, "atmel,flexcom-mode", &ddata->opmode);
50 if (err)
51 return err;
52
53 if (ddata->opmode < ATMEL_FLEXCOM_MODE_USART ||
54 ddata->opmode > ATMEL_FLEXCOM_MODE_TWI)
55 return -EINVAL;
56
57 ddata->base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
58 if (IS_ERR(ddata->base))
59 return PTR_ERR(ddata->base);
60
61 ddata->clk = devm_clk_get(&pdev->dev, NULL);
62 if (IS_ERR(ddata->clk))
63 return PTR_ERR(ddata->clk);
64
65 err = clk_prepare_enable(ddata->clk);
66 if (err)
67 return err;
68
69 /*
70 * Set the Operating Mode in the Mode Register: only the selected device
71 * is clocked. Hence, registers of the other serial devices remain
72 * inaccessible and are read as zero. Also the external I/O lines of the
73 * Flexcom are muxed to reach the selected device.
74 */
75 writel(FLEX_MR_OPMODE(ddata->opmode), ddata->base + FLEX_MR);
76
77 clk_disable_unprepare(ddata->clk);
78
79 return devm_of_platform_populate(&pdev->dev);
80}
81
82static const struct of_device_id atmel_flexcom_of_match[] = {
83 { .compatible = "atmel,sama5d2-flexcom" },
84 { /* sentinel */ }
85};
86MODULE_DEVICE_TABLE(of, atmel_flexcom_of_match);
87
88static int __maybe_unused atmel_flexcom_resume_noirq(struct device *dev)
89{
90 struct atmel_flexcom *ddata = dev_get_drvdata(dev);
91 int err;
92 u32 val;
93
94 err = clk_prepare_enable(ddata->clk);
95 if (err)
96 return err;
97
98 val = FLEX_MR_OPMODE(ddata->opmode),
99 writel(val, ddata->base + FLEX_MR);
100
101 clk_disable_unprepare(ddata->clk);
102
103 return 0;
104}
105
106static const struct dev_pm_ops __maybe_unused atmel_flexcom_pm_ops = {
107 .resume_noirq = atmel_flexcom_resume_noirq,
108};
109
110static struct platform_driver atmel_flexcom_driver = {
111 .probe = atmel_flexcom_probe,
112 .driver = {
113 .name = "atmel_flexcom",
114 .pm = pm_ptr(&atmel_flexcom_pm_ops),
115 .of_match_table = atmel_flexcom_of_match,
116 },
117};
118
119module_platform_driver(atmel_flexcom_driver);
120
121MODULE_AUTHOR("Cyrille Pitchen <cyrille.pitchen@atmel.com>");
122MODULE_DESCRIPTION("Atmel Flexcom MFD driver");
123MODULE_LICENSE("GPL v2");
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
43static int atmel_flexcom_probe(struct platform_device *pdev)
44{
45 struct device_node *np = pdev->dev.of_node;
46 struct clk *clk;
47 struct resource *res;
48 void __iomem *base;
49 u32 opmode;
50 int err;
51
52 err = of_property_read_u32(np, "atmel,flexcom-mode", &opmode);
53 if (err)
54 return err;
55
56 if (opmode < ATMEL_FLEXCOM_MODE_USART ||
57 opmode > ATMEL_FLEXCOM_MODE_TWI)
58 return -EINVAL;
59
60 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
61 base = devm_ioremap_resource(&pdev->dev, res);
62 if (IS_ERR(base))
63 return PTR_ERR(base);
64
65 clk = devm_clk_get(&pdev->dev, NULL);
66 if (IS_ERR(clk))
67 return PTR_ERR(clk);
68
69 err = clk_prepare_enable(clk);
70 if (err)
71 return err;
72
73 /*
74 * Set the Operating Mode in the Mode Register: only the selected device
75 * is clocked. Hence, registers of the other serial devices remain
76 * inaccessible and are read as zero. Also the external I/O lines of the
77 * Flexcom are muxed to reach the selected device.
78 */
79 writel(FLEX_MR_OPMODE(opmode), base + FLEX_MR);
80
81 clk_disable_unprepare(clk);
82
83 return of_platform_populate(np, NULL, NULL, &pdev->dev);
84}
85
86static const struct of_device_id atmel_flexcom_of_match[] = {
87 { .compatible = "atmel,sama5d2-flexcom" },
88 { /* sentinel */ }
89};
90MODULE_DEVICE_TABLE(of, atmel_flexcom_of_match);
91
92static struct platform_driver atmel_flexcom_driver = {
93 .probe = atmel_flexcom_probe,
94 .driver = {
95 .name = "atmel_flexcom",
96 .of_match_table = atmel_flexcom_of_match,
97 },
98};
99
100module_platform_driver(atmel_flexcom_driver);
101
102MODULE_AUTHOR("Cyrille Pitchen <cyrille.pitchen@atmel.com>");
103MODULE_DESCRIPTION("Atmel Flexcom MFD driver");
104MODULE_LICENSE("GPL v2");