Linux Audio

Check our new training course

Linux BSP development engineering services

Need help to port Linux and bootloaders to your hardware?
Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * 74xx MMIO GPIO driver
  4 *
  5 *  Copyright (C) 2014 Alexander Shiyan <shc_work@mail.ru>
 
 
 
 
 
  6 */
  7
  8#include <linux/bits.h>
  9#include <linux/err.h>
 10#include <linux/gpio/driver.h>
 11#include <linux/mod_devicetable.h>
 12#include <linux/module.h>
 
 
 13#include <linux/platform_device.h>
 14#include <linux/property.h>
 15
 16#define MMIO_74XX_DIR_IN	BIT(8)
 17#define MMIO_74XX_DIR_OUT	BIT(9)
 18#define MMIO_74XX_BIT_CNT(x)	((x) & GENMASK(7, 0))
 19
 20struct mmio_74xx_gpio_priv {
 21	struct gpio_chip	gc;
 22	unsigned		flags;
 23};
 24
 25static const struct of_device_id mmio_74xx_gpio_ids[] = {
 26	{
 27		.compatible	= "ti,741g125",
 28		.data		= (const void *)(MMIO_74XX_DIR_IN | 1),
 29	},
 30	{
 31		.compatible	= "ti,742g125",
 32		.data		= (const void *)(MMIO_74XX_DIR_IN | 2),
 33	},
 34	{
 35		.compatible	= "ti,74125",
 36		.data		= (const void *)(MMIO_74XX_DIR_IN | 4),
 37	},
 38	{
 39		.compatible	= "ti,74365",
 40		.data		= (const void *)(MMIO_74XX_DIR_IN | 6),
 41	},
 42	{
 43		.compatible	= "ti,74244",
 44		.data		= (const void *)(MMIO_74XX_DIR_IN | 8),
 45	},
 46	{
 47		.compatible	= "ti,741624",
 48		.data		= (const void *)(MMIO_74XX_DIR_IN | 16),
 49	},
 50	{
 51		.compatible	= "ti,741g74",
 52		.data		= (const void *)(MMIO_74XX_DIR_OUT | 1),
 53	},
 54	{
 55		.compatible	= "ti,7474",
 56		.data		= (const void *)(MMIO_74XX_DIR_OUT | 2),
 57	},
 58	{
 59		.compatible	= "ti,74175",
 60		.data		= (const void *)(MMIO_74XX_DIR_OUT | 4),
 61	},
 62	{
 63		.compatible	= "ti,74174",
 64		.data		= (const void *)(MMIO_74XX_DIR_OUT | 6),
 65	},
 66	{
 67		.compatible	= "ti,74273",
 68		.data		= (const void *)(MMIO_74XX_DIR_OUT | 8),
 69	},
 70	{
 71		.compatible	= "ti,7416374",
 72		.data		= (const void *)(MMIO_74XX_DIR_OUT | 16),
 73	},
 74	{ }
 75};
 76MODULE_DEVICE_TABLE(of, mmio_74xx_gpio_ids);
 77
 78static int mmio_74xx_get_direction(struct gpio_chip *gc, unsigned offset)
 79{
 80	struct mmio_74xx_gpio_priv *priv = gpiochip_get_data(gc);
 81
 82	if (priv->flags & MMIO_74XX_DIR_OUT)
 83		return GPIO_LINE_DIRECTION_OUT;
 84
 85	return  GPIO_LINE_DIRECTION_IN;
 86}
 87
 88static int mmio_74xx_dir_in(struct gpio_chip *gc, unsigned int gpio)
 89{
 90	struct mmio_74xx_gpio_priv *priv = gpiochip_get_data(gc);
 91
 92	if (priv->flags & MMIO_74XX_DIR_IN)
 93		return 0;
 94
 95	return -ENOTSUPP;
 96}
 97
 98static int mmio_74xx_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
 99{
100	struct mmio_74xx_gpio_priv *priv = gpiochip_get_data(gc);
101
102	if (priv->flags & MMIO_74XX_DIR_OUT) {
103		gc->set(gc, gpio, val);
104		return 0;
105	}
106
107	return -ENOTSUPP;
108}
109
110static int mmio_74xx_gpio_probe(struct platform_device *pdev)
111{
 
112	struct mmio_74xx_gpio_priv *priv;
 
113	void __iomem *dat;
114	int err;
115
 
 
 
 
116	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
117	if (!priv)
118		return -ENOMEM;
119
120	priv->flags = (uintptr_t)device_get_match_data(&pdev->dev);
121
122	dat = devm_platform_ioremap_resource(pdev, 0);
123	if (IS_ERR(dat))
124		return PTR_ERR(dat);
 
 
125
126	err = bgpio_init(&priv->gc, &pdev->dev,
127			 DIV_ROUND_UP(MMIO_74XX_BIT_CNT(priv->flags), 8),
128			 dat, NULL, NULL, NULL, NULL, 0);
129	if (err)
130		return err;
131
132	priv->gc.direction_input = mmio_74xx_dir_in;
133	priv->gc.direction_output = mmio_74xx_dir_out;
134	priv->gc.get_direction = mmio_74xx_get_direction;
135	priv->gc.ngpio = MMIO_74XX_BIT_CNT(priv->flags);
136	priv->gc.owner = THIS_MODULE;
137
138	platform_set_drvdata(pdev, priv);
139
140	return devm_gpiochip_add_data(&pdev->dev, &priv->gc, priv);
141}
142
143static struct platform_driver mmio_74xx_gpio_driver = {
144	.driver	= {
145		.name		= "74xx-mmio-gpio",
146		.of_match_table	= mmio_74xx_gpio_ids,
147	},
148	.probe	= mmio_74xx_gpio_probe,
149};
150module_platform_driver(mmio_74xx_gpio_driver);
151
152MODULE_LICENSE("GPL");
153MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
154MODULE_DESCRIPTION("74xx MMIO GPIO driver");
v4.6
 
  1/*
  2 * 74xx MMIO GPIO driver
  3 *
  4 *  Copyright (C) 2014 Alexander Shiyan <shc_work@mail.ru>
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License as published by
  8 * the Free Software Foundation; either version 2 of the License, or
  9 * (at your option) any later version.
 10 */
 11
 
 12#include <linux/err.h>
 
 
 13#include <linux/module.h>
 14#include <linux/of_device.h>
 15#include <linux/gpio/driver.h>
 16#include <linux/platform_device.h>
 
 17
 18#define MMIO_74XX_DIR_IN	(0 << 8)
 19#define MMIO_74XX_DIR_OUT	(1 << 8)
 20#define MMIO_74XX_BIT_CNT(x)	((x) & 0xff)
 21
 22struct mmio_74xx_gpio_priv {
 23	struct gpio_chip	gc;
 24	unsigned		flags;
 25};
 26
 27static const struct of_device_id mmio_74xx_gpio_ids[] = {
 28	{
 29		.compatible	= "ti,741g125",
 30		.data		= (const void *)(MMIO_74XX_DIR_IN | 1),
 31	},
 32	{
 33		.compatible	= "ti,742g125",
 34		.data		= (const void *)(MMIO_74XX_DIR_IN | 2),
 35	},
 36	{
 37		.compatible	= "ti,74125",
 38		.data		= (const void *)(MMIO_74XX_DIR_IN | 4),
 39	},
 40	{
 41		.compatible	= "ti,74365",
 42		.data		= (const void *)(MMIO_74XX_DIR_IN | 6),
 43	},
 44	{
 45		.compatible	= "ti,74244",
 46		.data		= (const void *)(MMIO_74XX_DIR_IN | 8),
 47	},
 48	{
 49		.compatible	= "ti,741624",
 50		.data		= (const void *)(MMIO_74XX_DIR_IN | 16),
 51	},
 52	{
 53		.compatible	= "ti,741g74",
 54		.data		= (const void *)(MMIO_74XX_DIR_OUT | 1),
 55	},
 56	{
 57		.compatible	= "ti,7474",
 58		.data		= (const void *)(MMIO_74XX_DIR_OUT | 2),
 59	},
 60	{
 61		.compatible	= "ti,74175",
 62		.data		= (const void *)(MMIO_74XX_DIR_OUT | 4),
 63	},
 64	{
 65		.compatible	= "ti,74174",
 66		.data		= (const void *)(MMIO_74XX_DIR_OUT | 6),
 67	},
 68	{
 69		.compatible	= "ti,74273",
 70		.data		= (const void *)(MMIO_74XX_DIR_OUT | 8),
 71	},
 72	{
 73		.compatible	= "ti,7416374",
 74		.data		= (const void *)(MMIO_74XX_DIR_OUT | 16),
 75	},
 76	{ }
 77};
 78MODULE_DEVICE_TABLE(of, mmio_74xx_gpio_ids);
 79
 80static int mmio_74xx_get_direction(struct gpio_chip *gc, unsigned offset)
 81{
 82	struct mmio_74xx_gpio_priv *priv = gpiochip_get_data(gc);
 83
 84	return !(priv->flags & MMIO_74XX_DIR_OUT);
 
 
 
 85}
 86
 87static int mmio_74xx_dir_in(struct gpio_chip *gc, unsigned int gpio)
 88{
 89	struct mmio_74xx_gpio_priv *priv = gpiochip_get_data(gc);
 90
 91	return (priv->flags & MMIO_74XX_DIR_OUT) ? -ENOTSUPP : 0;
 
 
 
 92}
 93
 94static int mmio_74xx_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
 95{
 96	struct mmio_74xx_gpio_priv *priv = gpiochip_get_data(gc);
 97
 98	if (priv->flags & MMIO_74XX_DIR_OUT) {
 99		gc->set(gc, gpio, val);
100		return 0;
101	}
102
103	return -ENOTSUPP;
104}
105
106static int mmio_74xx_gpio_probe(struct platform_device *pdev)
107{
108	const struct of_device_id *of_id;
109	struct mmio_74xx_gpio_priv *priv;
110	struct resource *res;
111	void __iomem *dat;
112	int err;
113
114	of_id = of_match_device(mmio_74xx_gpio_ids, &pdev->dev);
115	if (!of_id)
116		return -ENODEV;
117
118	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
119	if (!priv)
120		return -ENOMEM;
121
122	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
123	dat = devm_ioremap_resource(&pdev->dev, res);
 
124	if (IS_ERR(dat))
125		return PTR_ERR(dat);
126
127	priv->flags = (uintptr_t) of_id->data;
128
129	err = bgpio_init(&priv->gc, &pdev->dev,
130			 DIV_ROUND_UP(MMIO_74XX_BIT_CNT(priv->flags), 8),
131			 dat, NULL, NULL, NULL, NULL, 0);
132	if (err)
133		return err;
134
135	priv->gc.direction_input = mmio_74xx_dir_in;
136	priv->gc.direction_output = mmio_74xx_dir_out;
137	priv->gc.get_direction = mmio_74xx_get_direction;
138	priv->gc.ngpio = MMIO_74XX_BIT_CNT(priv->flags);
139	priv->gc.owner = THIS_MODULE;
140
141	platform_set_drvdata(pdev, priv);
142
143	return devm_gpiochip_add_data(&pdev->dev, &priv->gc, priv);
144}
145
146static struct platform_driver mmio_74xx_gpio_driver = {
147	.driver	= {
148		.name		= "74xx-mmio-gpio",
149		.of_match_table	= mmio_74xx_gpio_ids,
150	},
151	.probe	= mmio_74xx_gpio_probe,
152};
153module_platform_driver(mmio_74xx_gpio_driver);
154
155MODULE_LICENSE("GPL");
156MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
157MODULE_DESCRIPTION("74xx MMIO GPIO driver");