blob: e82543bcfdc828502dd4124262be0650dfcb7b01 [file] [log] [blame]
Boris Brezillon2c86e9f2014-10-06 15:48:43 +02001/*
2 * Copyright (C) 2014 Free Electrons
3 * Copyright (C) 2014 Atmel
4 *
5 * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <linux/clk.h>
Boris Brezillonea31c0c2015-07-07 19:16:43 +020021#include <linux/iopoll.h>
Boris Brezillon2c86e9f2014-10-06 15:48:43 +020022#include <linux/mfd/atmel-hlcdc.h>
23#include <linux/mfd/core.h>
24#include <linux/module.h>
Randy Dunlapac316722018-06-19 22:47:28 -070025#include <linux/mod_devicetable.h>
Boris Brezillon2c86e9f2014-10-06 15:48:43 +020026#include <linux/platform_device.h>
27#include <linux/regmap.h>
28
29#define ATMEL_HLCDC_REG_MAX (0x4000 - 0x4)
30
Boris Brezillonea31c0c2015-07-07 19:16:43 +020031struct atmel_hlcdc_regmap {
32 void __iomem *regs;
33};
34
Boris Brezillon2c86e9f2014-10-06 15:48:43 +020035static const struct mfd_cell atmel_hlcdc_cells[] = {
36 {
37 .name = "atmel-hlcdc-pwm",
38 .of_compatible = "atmel,hlcdc-pwm",
39 },
40 {
41 .name = "atmel-hlcdc-dc",
42 .of_compatible = "atmel,hlcdc-display-controller",
43 },
44};
45
Boris Brezillonea31c0c2015-07-07 19:16:43 +020046static int regmap_atmel_hlcdc_reg_write(void *context, unsigned int reg,
47 unsigned int val)
48{
49 struct atmel_hlcdc_regmap *hregmap = context;
50
51 if (reg <= ATMEL_HLCDC_DIS) {
52 u32 status;
53
Boris Brezillon2c2469b2016-09-06 14:19:29 +020054 readl_poll_timeout_atomic(hregmap->regs + ATMEL_HLCDC_SR,
55 status, !(status & ATMEL_HLCDC_SIP),
56 1, 100);
Boris Brezillonea31c0c2015-07-07 19:16:43 +020057 }
58
59 writel(val, hregmap->regs + reg);
60
61 return 0;
62}
63
64static int regmap_atmel_hlcdc_reg_read(void *context, unsigned int reg,
65 unsigned int *val)
66{
67 struct atmel_hlcdc_regmap *hregmap = context;
68
69 *val = readl(hregmap->regs + reg);
70
71 return 0;
72}
73
Boris Brezillon2c86e9f2014-10-06 15:48:43 +020074static const struct regmap_config atmel_hlcdc_regmap_config = {
75 .reg_bits = 32,
76 .val_bits = 32,
77 .reg_stride = 4,
78 .max_register = ATMEL_HLCDC_REG_MAX,
Boris Brezillonea31c0c2015-07-07 19:16:43 +020079 .reg_write = regmap_atmel_hlcdc_reg_write,
80 .reg_read = regmap_atmel_hlcdc_reg_read,
81 .fast_io = true,
Boris Brezillon2c86e9f2014-10-06 15:48:43 +020082};
83
84static int atmel_hlcdc_probe(struct platform_device *pdev)
85{
Boris Brezillonea31c0c2015-07-07 19:16:43 +020086 struct atmel_hlcdc_regmap *hregmap;
Boris Brezillon2c86e9f2014-10-06 15:48:43 +020087 struct device *dev = &pdev->dev;
88 struct atmel_hlcdc *hlcdc;
89 struct resource *res;
Boris Brezillonea31c0c2015-07-07 19:16:43 +020090
91 hregmap = devm_kzalloc(dev, sizeof(*hregmap), GFP_KERNEL);
92 if (!hregmap)
93 return -ENOMEM;
Boris Brezillon2c86e9f2014-10-06 15:48:43 +020094
95 hlcdc = devm_kzalloc(dev, sizeof(*hlcdc), GFP_KERNEL);
96 if (!hlcdc)
97 return -ENOMEM;
98
99 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Boris Brezillonea31c0c2015-07-07 19:16:43 +0200100 hregmap->regs = devm_ioremap_resource(dev, res);
101 if (IS_ERR(hregmap->regs))
102 return PTR_ERR(hregmap->regs);
Boris Brezillon2c86e9f2014-10-06 15:48:43 +0200103
104 hlcdc->irq = platform_get_irq(pdev, 0);
105 if (hlcdc->irq < 0)
106 return hlcdc->irq;
107
108 hlcdc->periph_clk = devm_clk_get(dev, "periph_clk");
109 if (IS_ERR(hlcdc->periph_clk)) {
110 dev_err(dev, "failed to get peripheral clock\n");
111 return PTR_ERR(hlcdc->periph_clk);
112 }
113
114 hlcdc->sys_clk = devm_clk_get(dev, "sys_clk");
115 if (IS_ERR(hlcdc->sys_clk)) {
116 dev_err(dev, "failed to get system clock\n");
117 return PTR_ERR(hlcdc->sys_clk);
118 }
119
120 hlcdc->slow_clk = devm_clk_get(dev, "slow_clk");
121 if (IS_ERR(hlcdc->slow_clk)) {
122 dev_err(dev, "failed to get slow clock\n");
123 return PTR_ERR(hlcdc->slow_clk);
124 }
125
Boris Brezillonea31c0c2015-07-07 19:16:43 +0200126 hlcdc->regmap = devm_regmap_init(dev, NULL, hregmap,
127 &atmel_hlcdc_regmap_config);
Boris Brezillon2c86e9f2014-10-06 15:48:43 +0200128 if (IS_ERR(hlcdc->regmap))
129 return PTR_ERR(hlcdc->regmap);
130
131 dev_set_drvdata(dev, hlcdc);
132
Laxman Dewanganf32fb9a2016-04-08 00:12:59 +0530133 return devm_mfd_add_devices(dev, -1, atmel_hlcdc_cells,
134 ARRAY_SIZE(atmel_hlcdc_cells),
135 NULL, 0, NULL);
Boris Brezillon2c86e9f2014-10-06 15:48:43 +0200136}
137
138static const struct of_device_id atmel_hlcdc_match[] = {
Boris Brezillond9c93f52015-07-31 18:45:54 +0200139 { .compatible = "atmel,at91sam9n12-hlcdc" },
140 { .compatible = "atmel,at91sam9x5-hlcdc" },
141 { .compatible = "atmel,sama5d2-hlcdc" },
Boris Brezillon2c86e9f2014-10-06 15:48:43 +0200142 { .compatible = "atmel,sama5d3-hlcdc" },
Boris Brezillond9c93f52015-07-31 18:45:54 +0200143 { .compatible = "atmel,sama5d4-hlcdc" },
Boris Brezillon2c86e9f2014-10-06 15:48:43 +0200144 { /* sentinel */ },
145};
Luis de Bethencourta0aef1f2015-09-17 20:15:44 +0200146MODULE_DEVICE_TABLE(of, atmel_hlcdc_match);
Boris Brezillon2c86e9f2014-10-06 15:48:43 +0200147
148static struct platform_driver atmel_hlcdc_driver = {
149 .probe = atmel_hlcdc_probe,
Boris Brezillon2c86e9f2014-10-06 15:48:43 +0200150 .driver = {
151 .name = "atmel-hlcdc",
152 .of_match_table = atmel_hlcdc_match,
153 },
154};
155module_platform_driver(atmel_hlcdc_driver);
156
157MODULE_ALIAS("platform:atmel-hlcdc");
158MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
159MODULE_DESCRIPTION("Atmel HLCDC driver");
160MODULE_LICENSE("GPL v2");