1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014 Free Electrons
4 * Copyright (C) 2014 Atmel
6 * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
10 #include <linux/iopoll.h>
11 #include <linux/mfd/atmel-hlcdc.h>
12 #include <linux/mfd/core.h>
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
18 #define ATMEL_HLCDC_REG_MAX (0x4000 - 0x4)
20 struct atmel_hlcdc_regmap
{
25 static const struct mfd_cell atmel_hlcdc_cells
[] = {
27 .name
= "atmel-hlcdc-pwm",
28 .of_compatible
= "atmel,hlcdc-pwm",
31 .name
= "atmel-hlcdc-dc",
32 .of_compatible
= "atmel,hlcdc-display-controller",
36 static int regmap_atmel_hlcdc_reg_write(void *context
, unsigned int reg
,
39 struct atmel_hlcdc_regmap
*hregmap
= context
;
41 if (reg
<= ATMEL_HLCDC_DIS
) {
45 ret
= readl_poll_timeout_atomic(hregmap
->regs
+ ATMEL_HLCDC_SR
,
47 !(status
& ATMEL_HLCDC_SIP
),
51 "Timeout! Clock domain synchronization is in progress!\n");
56 writel(val
, hregmap
->regs
+ reg
);
61 static int regmap_atmel_hlcdc_reg_read(void *context
, unsigned int reg
,
64 struct atmel_hlcdc_regmap
*hregmap
= context
;
66 *val
= readl(hregmap
->regs
+ reg
);
71 static const struct regmap_config atmel_hlcdc_regmap_config
= {
75 .max_register
= ATMEL_HLCDC_REG_MAX
,
76 .reg_write
= regmap_atmel_hlcdc_reg_write
,
77 .reg_read
= regmap_atmel_hlcdc_reg_read
,
81 static int atmel_hlcdc_probe(struct platform_device
*pdev
)
83 struct atmel_hlcdc_regmap
*hregmap
;
84 struct device
*dev
= &pdev
->dev
;
85 struct atmel_hlcdc
*hlcdc
;
87 hregmap
= devm_kzalloc(dev
, sizeof(*hregmap
), GFP_KERNEL
);
91 hlcdc
= devm_kzalloc(dev
, sizeof(*hlcdc
), GFP_KERNEL
);
95 hregmap
->regs
= devm_platform_ioremap_resource(pdev
, 0);
96 if (IS_ERR(hregmap
->regs
))
97 return PTR_ERR(hregmap
->regs
);
99 hregmap
->dev
= &pdev
->dev
;
101 hlcdc
->irq
= platform_get_irq(pdev
, 0);
105 hlcdc
->periph_clk
= devm_clk_get(dev
, "periph_clk");
106 if (IS_ERR(hlcdc
->periph_clk
)) {
107 dev_err(dev
, "failed to get peripheral clock\n");
108 return PTR_ERR(hlcdc
->periph_clk
);
111 hlcdc
->sys_clk
= devm_clk_get(dev
, "sys_clk");
112 if (IS_ERR(hlcdc
->sys_clk
)) {
113 dev_err(dev
, "failed to get system clock\n");
114 return PTR_ERR(hlcdc
->sys_clk
);
117 hlcdc
->slow_clk
= devm_clk_get(dev
, "slow_clk");
118 if (IS_ERR(hlcdc
->slow_clk
)) {
119 dev_err(dev
, "failed to get slow clock\n");
120 return PTR_ERR(hlcdc
->slow_clk
);
123 hlcdc
->regmap
= devm_regmap_init(dev
, NULL
, hregmap
,
124 &atmel_hlcdc_regmap_config
);
125 if (IS_ERR(hlcdc
->regmap
))
126 return PTR_ERR(hlcdc
->regmap
);
128 dev_set_drvdata(dev
, hlcdc
);
130 return devm_mfd_add_devices(dev
, -1, atmel_hlcdc_cells
,
131 ARRAY_SIZE(atmel_hlcdc_cells
),
135 static const struct of_device_id atmel_hlcdc_match
[] = {
136 { .compatible
= "atmel,at91sam9n12-hlcdc" },
137 { .compatible
= "atmel,at91sam9x5-hlcdc" },
138 { .compatible
= "atmel,sama5d2-hlcdc" },
139 { .compatible
= "atmel,sama5d3-hlcdc" },
140 { .compatible
= "atmel,sama5d4-hlcdc" },
141 { .compatible
= "microchip,sam9x60-hlcdc" },
142 { .compatible
= "microchip,sam9x75-xlcdc" },
145 MODULE_DEVICE_TABLE(of
, atmel_hlcdc_match
);
147 static struct platform_driver atmel_hlcdc_driver
= {
148 .probe
= atmel_hlcdc_probe
,
150 .name
= "atmel-hlcdc",
151 .of_match_table
= atmel_hlcdc_match
,
154 module_platform_driver(atmel_hlcdc_driver
);
156 MODULE_ALIAS("platform:atmel-hlcdc");
157 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
158 MODULE_DESCRIPTION("Atmel HLCDC driver");
159 MODULE_LICENSE("GPL v2");