Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  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
 
 
 
 
 
 
 
 10 */
 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/platform_device.h>
 18#include <linux/slab.h>
 19#include <linux/clk.h>
 20
 21#define IIM_BANK_BASE(n)	(0x800 + 0x400 * (n))
 22
 23struct imx_iim_drvdata {
 24	unsigned int nregs;
 25};
 26
 27struct iim_priv {
 28	void __iomem *base;
 29	struct clk *clk;
 30};
 31
 32static int imx_iim_read(void *context, unsigned int offset,
 33			  void *buf, size_t bytes)
 34{
 35	struct iim_priv *iim = context;
 36	int i, ret;
 37	u8 *buf8 = buf;
 38
 39	ret = clk_prepare_enable(iim->clk);
 40	if (ret)
 41		return ret;
 42
 43	for (i = offset; i < offset + bytes; i++) {
 44		int bank = i >> 5;
 45		int reg = i & 0x1f;
 46
 47		*buf8++ = readl(iim->base + IIM_BANK_BASE(bank) + reg * 4);
 48	}
 49
 50	clk_disable_unprepare(iim->clk);
 51
 52	return 0;
 53}
 54
 55static struct imx_iim_drvdata imx27_drvdata = {
 56	.nregs = 2 * 32,
 57};
 58
 59static struct imx_iim_drvdata imx25_imx31_imx35_drvdata = {
 60	.nregs = 3 * 32,
 61};
 62
 63static struct imx_iim_drvdata imx51_drvdata = {
 64	.nregs = 4 * 32,
 65};
 66
 67static struct imx_iim_drvdata imx53_drvdata = {
 68	.nregs = 4 * 32 + 16,
 69};
 70
 71static const struct of_device_id imx_iim_dt_ids[] = {
 72	{
 73		.compatible = "fsl,imx25-iim",
 74		.data = &imx25_imx31_imx35_drvdata,
 75	}, {
 76		.compatible = "fsl,imx27-iim",
 77		.data = &imx27_drvdata,
 78	}, {
 79		.compatible = "fsl,imx31-iim",
 80		.data = &imx25_imx31_imx35_drvdata,
 81	}, {
 82		.compatible = "fsl,imx35-iim",
 83		.data = &imx25_imx31_imx35_drvdata,
 84	}, {
 85		.compatible = "fsl,imx51-iim",
 86		.data = &imx51_drvdata,
 87	}, {
 88		.compatible = "fsl,imx53-iim",
 89		.data = &imx53_drvdata,
 90	}, {
 91		/* sentinel */
 92	},
 93};
 94MODULE_DEVICE_TABLE(of, imx_iim_dt_ids);
 95
 96static int imx_iim_probe(struct platform_device *pdev)
 97{
 
 98	struct device *dev = &pdev->dev;
 
 99	struct iim_priv *iim;
100	struct nvmem_device *nvmem;
101	struct nvmem_config cfg = {};
102	const struct imx_iim_drvdata *drvdata = NULL;
103
104	iim = devm_kzalloc(dev, sizeof(*iim), GFP_KERNEL);
105	if (!iim)
106		return -ENOMEM;
107
108	iim->base = devm_platform_ioremap_resource(pdev, 0);
 
109	if (IS_ERR(iim->base))
110		return PTR_ERR(iim->base);
111
112	drvdata = of_device_get_match_data(&pdev->dev);
 
 
 
 
113
114	iim->clk = devm_clk_get(dev, NULL);
115	if (IS_ERR(iim->clk))
116		return PTR_ERR(iim->clk);
117
118	cfg.name = "imx-iim";
119	cfg.read_only = true;
120	cfg.word_size = 1;
121	cfg.stride = 1;
122	cfg.reg_read = imx_iim_read;
123	cfg.dev = dev;
124	cfg.size = drvdata->nregs;
125	cfg.priv = iim;
126
127	nvmem = devm_nvmem_register(dev, &cfg);
128
129	return PTR_ERR_OR_ZERO(nvmem);
130}
131
132static struct platform_driver imx_iim_driver = {
133	.probe	= imx_iim_probe,
134	.driver = {
135		.name	= "imx-iim",
136		.of_match_table = imx_iim_dt_ids,
137	},
138};
139module_platform_driver(imx_iim_driver);
140
141MODULE_AUTHOR("Michael Grzeschik <m.grzeschik@pengutronix.de>");
142MODULE_DESCRIPTION("i.MX IIM driver");
143MODULE_LICENSE("GPL v2");
v4.17
 
  1/*
  2 * i.MX IIM driver
  3 *
  4 * Copyright (c) 2017 Pengutronix, Michael Grzeschik <m.grzeschik@pengutronix.de>
  5 *
  6 * Based on the barebox iim driver,
  7 * Copyright (c) 2010 Baruch Siach <baruch@tkos.co.il>,
  8 *	Orex Computed Radiography
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License version 2
 12 * as published by the Free Software Foundation.
 13 *
 14 * http://www.opensource.org/licenses/gpl-license.html
 15 * http://www.gnu.org/copyleft/gpl.html
 16 */
 17
 18#include <linux/device.h>
 19#include <linux/io.h>
 20#include <linux/module.h>
 21#include <linux/nvmem-provider.h>
 22#include <linux/of.h>
 23#include <linux/of_device.h>
 24#include <linux/platform_device.h>
 25#include <linux/slab.h>
 26#include <linux/clk.h>
 27
 28#define IIM_BANK_BASE(n)	(0x800 + 0x400 * (n))
 29
 30struct imx_iim_drvdata {
 31	unsigned int nregs;
 32};
 33
 34struct iim_priv {
 35	void __iomem *base;
 36	struct clk *clk;
 37};
 38
 39static int imx_iim_read(void *context, unsigned int offset,
 40			  void *buf, size_t bytes)
 41{
 42	struct iim_priv *iim = context;
 43	int i, ret;
 44	u8 *buf8 = buf;
 45
 46	ret = clk_prepare_enable(iim->clk);
 47	if (ret)
 48		return ret;
 49
 50	for (i = offset; i < offset + bytes; i++) {
 51		int bank = i >> 5;
 52		int reg = i & 0x1f;
 53
 54		*buf8++ = readl(iim->base + IIM_BANK_BASE(bank) + reg * 4);
 55	}
 56
 57	clk_disable_unprepare(iim->clk);
 58
 59	return 0;
 60}
 61
 62static struct imx_iim_drvdata imx27_drvdata = {
 63	.nregs = 2 * 32,
 64};
 65
 66static struct imx_iim_drvdata imx25_imx31_imx35_drvdata = {
 67	.nregs = 3 * 32,
 68};
 69
 70static struct imx_iim_drvdata imx51_drvdata = {
 71	.nregs = 4 * 32,
 72};
 73
 74static struct imx_iim_drvdata imx53_drvdata = {
 75	.nregs = 4 * 32 + 16,
 76};
 77
 78static const struct of_device_id imx_iim_dt_ids[] = {
 79	{
 80		.compatible = "fsl,imx25-iim",
 81		.data = &imx25_imx31_imx35_drvdata,
 82	}, {
 83		.compatible = "fsl,imx27-iim",
 84		.data = &imx27_drvdata,
 85	}, {
 86		.compatible = "fsl,imx31-iim",
 87		.data = &imx25_imx31_imx35_drvdata,
 88	}, {
 89		.compatible = "fsl,imx35-iim",
 90		.data = &imx25_imx31_imx35_drvdata,
 91	}, {
 92		.compatible = "fsl,imx51-iim",
 93		.data = &imx51_drvdata,
 94	}, {
 95		.compatible = "fsl,imx53-iim",
 96		.data = &imx53_drvdata,
 97	}, {
 98		/* sentinel */
 99	},
100};
101MODULE_DEVICE_TABLE(of, imx_iim_dt_ids);
102
103static int imx_iim_probe(struct platform_device *pdev)
104{
105	const struct of_device_id *of_id;
106	struct device *dev = &pdev->dev;
107	struct resource *res;
108	struct iim_priv *iim;
109	struct nvmem_device *nvmem;
110	struct nvmem_config cfg = {};
111	const struct imx_iim_drvdata *drvdata = NULL;
112
113	iim = devm_kzalloc(dev, sizeof(*iim), GFP_KERNEL);
114	if (!iim)
115		return -ENOMEM;
116
117	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
118	iim->base = devm_ioremap_resource(dev, res);
119	if (IS_ERR(iim->base))
120		return PTR_ERR(iim->base);
121
122	of_id = of_match_device(imx_iim_dt_ids, dev);
123	if (!of_id)
124		return -ENODEV;
125
126	drvdata = of_id->data;
127
128	iim->clk = devm_clk_get(dev, NULL);
129	if (IS_ERR(iim->clk))
130		return PTR_ERR(iim->clk);
131
132	cfg.name = "imx-iim",
133	cfg.read_only = true,
134	cfg.word_size = 1,
135	cfg.stride = 1,
136	cfg.reg_read = imx_iim_read,
137	cfg.dev = dev;
138	cfg.size = drvdata->nregs;
139	cfg.priv = iim;
140
141	nvmem = devm_nvmem_register(dev, &cfg);
142
143	return PTR_ERR_OR_ZERO(nvmem);
144}
145
146static struct platform_driver imx_iim_driver = {
147	.probe	= imx_iim_probe,
148	.driver = {
149		.name	= "imx-iim",
150		.of_match_table = imx_iim_dt_ids,
151	},
152};
153module_platform_driver(imx_iim_driver);
154
155MODULE_AUTHOR("Michael Grzeschik <m.grzeschik@pengutronix.de>");
156MODULE_DESCRIPTION("i.MX IIM driver");
157MODULE_LICENSE("GPL v2");