2 * Copyright (C) 2014 Free Electrons
3 * Copyright (C) 2014 Atmel
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.
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
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/>.
20 #include <linux/clk.h>
21 #include <linux/iopoll.h>
22 #include <linux/mfd/atmel-hlcdc.h>
23 #include <linux/mfd/core.h>
24 #include <linux/module.h>
25 #include <linux/mod_devicetable.h>
26 #include <linux/platform_device.h>
27 #include <linux/regmap.h>
29 #define ATMEL_HLCDC_REG_MAX (0x4000 - 0x4)
31 struct atmel_hlcdc_regmap {
35 static const struct mfd_cell atmel_hlcdc_cells[] = {
37 .name = "atmel-hlcdc-pwm",
38 .of_compatible = "atmel,hlcdc-pwm",
41 .name = "atmel-hlcdc-dc",
42 .of_compatible = "atmel,hlcdc-display-controller",
46 static int regmap_atmel_hlcdc_reg_write(void *context, unsigned int reg,
49 struct atmel_hlcdc_regmap *hregmap = context;
51 if (reg <= ATMEL_HLCDC_DIS) {
54 readl_poll_timeout_atomic(hregmap->regs + ATMEL_HLCDC_SR,
55 status, !(status & ATMEL_HLCDC_SIP),
59 writel(val, hregmap->regs + reg);
64 static int regmap_atmel_hlcdc_reg_read(void *context, unsigned int reg,
67 struct atmel_hlcdc_regmap *hregmap = context;
69 *val = readl(hregmap->regs + reg);
74 static const struct regmap_config atmel_hlcdc_regmap_config = {
78 .max_register = ATMEL_HLCDC_REG_MAX,
79 .reg_write = regmap_atmel_hlcdc_reg_write,
80 .reg_read = regmap_atmel_hlcdc_reg_read,
84 static int atmel_hlcdc_probe(struct platform_device *pdev)
86 struct atmel_hlcdc_regmap *hregmap;
87 struct device *dev = &pdev->dev;
88 struct atmel_hlcdc *hlcdc;
91 hregmap = devm_kzalloc(dev, sizeof(*hregmap), GFP_KERNEL);
95 hlcdc = devm_kzalloc(dev, sizeof(*hlcdc), GFP_KERNEL);
99 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
100 hregmap->regs = devm_ioremap_resource(dev, res);
101 if (IS_ERR(hregmap->regs))
102 return PTR_ERR(hregmap->regs);
104 hlcdc->irq = platform_get_irq(pdev, 0);
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);
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);
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);
126 hlcdc->regmap = devm_regmap_init(dev, NULL, hregmap,
127 &atmel_hlcdc_regmap_config);
128 if (IS_ERR(hlcdc->regmap))
129 return PTR_ERR(hlcdc->regmap);
131 dev_set_drvdata(dev, hlcdc);
133 return devm_mfd_add_devices(dev, -1, atmel_hlcdc_cells,
134 ARRAY_SIZE(atmel_hlcdc_cells),
138 static const struct of_device_id atmel_hlcdc_match[] = {
139 { .compatible = "atmel,at91sam9n12-hlcdc" },
140 { .compatible = "atmel,at91sam9x5-hlcdc" },
141 { .compatible = "atmel,sama5d2-hlcdc" },
142 { .compatible = "atmel,sama5d3-hlcdc" },
143 { .compatible = "atmel,sama5d4-hlcdc" },
146 MODULE_DEVICE_TABLE(of, atmel_hlcdc_match);
148 static struct platform_driver atmel_hlcdc_driver = {
149 .probe = atmel_hlcdc_probe,
151 .name = "atmel-hlcdc",
152 .of_match_table = atmel_hlcdc_match,
155 module_platform_driver(atmel_hlcdc_driver);
157 MODULE_ALIAS("platform:atmel-hlcdc");
159 MODULE_DESCRIPTION("Atmel HLCDC driver");
160 MODULE_LICENSE("GPL v2");