blob: c86339a7f583823e0a12ee5993f136e511fb7626 [file] [log] [blame]
Thomas Gleixneracee2e82019-06-04 10:10:53 +02001// SPDX-License-Identifier: GPL-2.0-only
Michael Grzeschikc066c1c2017-03-31 13:44:50 +01002/*
3 * i.MX IIM driver
4 *
5 * Copyright (c) 2017 Pengutronix, Michael Grzeschik <m.grzeschik@pengutronix.de>
6 *
7 * Based on the barebox iim driver,
8 * Copyright (c) 2010 Baruch Siach <baruch@tkos.co.il>,
9 * Orex Computed Radiography
Michael Grzeschikc066c1c2017-03-31 13:44:50 +010010 */
11
12#include <linux/device.h>
13#include <linux/io.h>
14#include <linux/module.h>
15#include <linux/nvmem-provider.h>
16#include <linux/of.h>
17#include <linux/of_device.h>
18#include <linux/platform_device.h>
19#include <linux/slab.h>
20#include <linux/clk.h>
21
22#define IIM_BANK_BASE(n) (0x800 + 0x400 * (n))
23
24struct imx_iim_drvdata {
25 unsigned int nregs;
26};
27
28struct iim_priv {
29 void __iomem *base;
30 struct clk *clk;
Michael Grzeschikc066c1c2017-03-31 13:44:50 +010031};
32
33static int imx_iim_read(void *context, unsigned int offset,
34 void *buf, size_t bytes)
35{
36 struct iim_priv *iim = context;
37 int i, ret;
38 u8 *buf8 = buf;
39
40 ret = clk_prepare_enable(iim->clk);
41 if (ret)
42 return ret;
43
44 for (i = offset; i < offset + bytes; i++) {
45 int bank = i >> 5;
46 int reg = i & 0x1f;
47
48 *buf8++ = readl(iim->base + IIM_BANK_BASE(bank) + reg * 4);
49 }
50
51 clk_disable_unprepare(iim->clk);
52
53 return 0;
54}
55
56static struct imx_iim_drvdata imx27_drvdata = {
57 .nregs = 2 * 32,
58};
59
60static struct imx_iim_drvdata imx25_imx31_imx35_drvdata = {
61 .nregs = 3 * 32,
62};
63
64static struct imx_iim_drvdata imx51_drvdata = {
65 .nregs = 4 * 32,
66};
67
68static struct imx_iim_drvdata imx53_drvdata = {
69 .nregs = 4 * 32 + 16,
70};
71
72static const struct of_device_id imx_iim_dt_ids[] = {
73 {
74 .compatible = "fsl,imx25-iim",
75 .data = &imx25_imx31_imx35_drvdata,
76 }, {
77 .compatible = "fsl,imx27-iim",
78 .data = &imx27_drvdata,
79 }, {
80 .compatible = "fsl,imx31-iim",
81 .data = &imx25_imx31_imx35_drvdata,
82 }, {
83 .compatible = "fsl,imx35-iim",
84 .data = &imx25_imx31_imx35_drvdata,
85 }, {
86 .compatible = "fsl,imx51-iim",
87 .data = &imx51_drvdata,
88 }, {
89 .compatible = "fsl,imx53-iim",
90 .data = &imx53_drvdata,
91 }, {
92 /* sentinel */
93 },
94};
95MODULE_DEVICE_TABLE(of, imx_iim_dt_ids);
96
97static int imx_iim_probe(struct platform_device *pdev)
98{
Michael Grzeschikc066c1c2017-03-31 13:44:50 +010099 struct device *dev = &pdev->dev;
Michael Grzeschikc066c1c2017-03-31 13:44:50 +0100100 struct iim_priv *iim;
101 struct nvmem_device *nvmem;
Masahiro Yamada01d35ca2017-10-21 01:57:38 +0900102 struct nvmem_config cfg = {};
Michael Grzeschikc066c1c2017-03-31 13:44:50 +0100103 const struct imx_iim_drvdata *drvdata = NULL;
104
105 iim = devm_kzalloc(dev, sizeof(*iim), GFP_KERNEL);
106 if (!iim)
107 return -ENOMEM;
108
Anson Huangededa042019-04-13 11:33:01 +0100109 iim->base = devm_platform_ioremap_resource(pdev, 0);
Michael Grzeschikc066c1c2017-03-31 13:44:50 +0100110 if (IS_ERR(iim->base))
111 return PTR_ERR(iim->base);
112
Fabio Estevam579db092021-01-29 17:14:27 +0000113 drvdata = of_device_get_match_data(&pdev->dev);
Michael Grzeschikc066c1c2017-03-31 13:44:50 +0100114
Andrey Smirnovb28b7382018-03-09 14:47:14 +0000115 iim->clk = devm_clk_get(dev, NULL);
Michael Grzeschikc066c1c2017-03-31 13:44:50 +0100116 if (IS_ERR(iim->clk))
117 return PTR_ERR(iim->clk);
118
Masahiro Yamada01d35ca2017-10-21 01:57:38 +0900119 cfg.name = "imx-iim",
120 cfg.read_only = true,
121 cfg.word_size = 1,
122 cfg.stride = 1,
Masahiro Yamada01d35ca2017-10-21 01:57:38 +0900123 cfg.reg_read = imx_iim_read,
124 cfg.dev = dev;
125 cfg.size = drvdata->nregs;
126 cfg.priv = iim;
Michael Grzeschikc066c1c2017-03-31 13:44:50 +0100127
Andrey Smirnov547a2c92018-03-09 14:47:07 +0000128 nvmem = devm_nvmem_register(dev, &cfg);
Michael Grzeschikc066c1c2017-03-31 13:44:50 +0100129
Andrey Smirnov547a2c92018-03-09 14:47:07 +0000130 return PTR_ERR_OR_ZERO(nvmem);
Michael Grzeschikc066c1c2017-03-31 13:44:50 +0100131}
132
133static struct platform_driver imx_iim_driver = {
134 .probe = imx_iim_probe,
Michael Grzeschikc066c1c2017-03-31 13:44:50 +0100135 .driver = {
136 .name = "imx-iim",
137 .of_match_table = imx_iim_dt_ids,
138 },
139};
140module_platform_driver(imx_iim_driver);
141
142MODULE_AUTHOR("Michael Grzeschik <m.grzeschik@pengutronix.de>");
143MODULE_DESCRIPTION("i.MX IIM driver");
144MODULE_LICENSE("GPL v2");