xref: /openbmc/linux/drivers/gpio/gpio-rc5t583.c (revision 867e6d38)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * GPIO driver for RICOH583 power management chip.
4  *
5  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
6  * Author: Laxman dewangan <ldewangan@nvidia.com>
7  *
8  * Based on code
9  *	Copyright (C) 2011 RICOH COMPANY,LTD
10  */
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/platform_device.h>
15 #include <linux/device.h>
16 #include <linux/gpio/driver.h>
17 #include <linux/mfd/rc5t583.h>
18 
19 struct rc5t583_gpio {
20 	struct gpio_chip gpio_chip;
21 	struct rc5t583 *rc5t583;
22 };
23 
24 static int rc5t583_gpio_get(struct gpio_chip *gc, unsigned int offset)
25 {
26 	struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
27 	struct device *parent = rc5t583_gpio->rc5t583->dev;
28 	uint8_t val = 0;
29 	int ret;
30 
31 	ret = rc5t583_read(parent, RC5T583_GPIO_MON_IOIN, &val);
32 	if (ret < 0)
33 		return ret;
34 
35 	return !!(val & BIT(offset));
36 }
37 
38 static void rc5t583_gpio_set(struct gpio_chip *gc, unsigned int offset, int val)
39 {
40 	struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
41 	struct device *parent = rc5t583_gpio->rc5t583->dev;
42 	if (val)
43 		rc5t583_set_bits(parent, RC5T583_GPIO_IOOUT, BIT(offset));
44 	else
45 		rc5t583_clear_bits(parent, RC5T583_GPIO_IOOUT, BIT(offset));
46 }
47 
48 static int rc5t583_gpio_dir_input(struct gpio_chip *gc, unsigned int offset)
49 {
50 	struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
51 	struct device *parent = rc5t583_gpio->rc5t583->dev;
52 	int ret;
53 
54 	ret = rc5t583_clear_bits(parent, RC5T583_GPIO_IOSEL, BIT(offset));
55 	if (ret < 0)
56 		return ret;
57 
58 	/* Set pin to gpio mode */
59 	return rc5t583_clear_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
60 }
61 
62 static int rc5t583_gpio_dir_output(struct gpio_chip *gc, unsigned offset,
63 			int value)
64 {
65 	struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
66 	struct device *parent = rc5t583_gpio->rc5t583->dev;
67 	int ret;
68 
69 	rc5t583_gpio_set(gc, offset, value);
70 	ret = rc5t583_set_bits(parent, RC5T583_GPIO_IOSEL, BIT(offset));
71 	if (ret < 0)
72 		return ret;
73 
74 	/* Set pin to gpio mode */
75 	return rc5t583_clear_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
76 }
77 
78 static int rc5t583_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
79 {
80 	struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
81 
82 	if (offset < RC5T583_MAX_GPIO)
83 		return rc5t583_gpio->rc5t583->irq_base +
84 				RC5T583_IRQ_GPIO0 + offset;
85 	return -EINVAL;
86 }
87 
88 static void rc5t583_gpio_free(struct gpio_chip *gc, unsigned offset)
89 {
90 	struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
91 	struct device *parent = rc5t583_gpio->rc5t583->dev;
92 
93 	rc5t583_set_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
94 }
95 
96 static int rc5t583_gpio_probe(struct platform_device *pdev)
97 {
98 	struct rc5t583 *rc5t583 = dev_get_drvdata(pdev->dev.parent);
99 	struct rc5t583_platform_data *pdata = dev_get_platdata(rc5t583->dev);
100 	struct rc5t583_gpio *rc5t583_gpio;
101 
102 	rc5t583_gpio = devm_kzalloc(&pdev->dev, sizeof(*rc5t583_gpio),
103 					GFP_KERNEL);
104 	if (!rc5t583_gpio)
105 		return -ENOMEM;
106 
107 	rc5t583_gpio->gpio_chip.label = "gpio-rc5t583",
108 	rc5t583_gpio->gpio_chip.owner = THIS_MODULE,
109 	rc5t583_gpio->gpio_chip.free = rc5t583_gpio_free,
110 	rc5t583_gpio->gpio_chip.direction_input = rc5t583_gpio_dir_input,
111 	rc5t583_gpio->gpio_chip.direction_output = rc5t583_gpio_dir_output,
112 	rc5t583_gpio->gpio_chip.set = rc5t583_gpio_set,
113 	rc5t583_gpio->gpio_chip.get = rc5t583_gpio_get,
114 	rc5t583_gpio->gpio_chip.to_irq = rc5t583_gpio_to_irq,
115 	rc5t583_gpio->gpio_chip.ngpio = RC5T583_MAX_GPIO,
116 	rc5t583_gpio->gpio_chip.can_sleep = true,
117 	rc5t583_gpio->gpio_chip.parent = &pdev->dev;
118 	rc5t583_gpio->gpio_chip.base = -1;
119 	rc5t583_gpio->rc5t583 = rc5t583;
120 
121 	if (pdata && pdata->gpio_base)
122 		rc5t583_gpio->gpio_chip.base = pdata->gpio_base;
123 
124 	platform_set_drvdata(pdev, rc5t583_gpio);
125 
126 	return devm_gpiochip_add_data(&pdev->dev, &rc5t583_gpio->gpio_chip,
127 				      rc5t583_gpio);
128 }
129 
130 static struct platform_driver rc5t583_gpio_driver = {
131 	.driver = {
132 		.name    = "rc5t583-gpio",
133 	},
134 	.probe		= rc5t583_gpio_probe,
135 };
136 
137 static int __init rc5t583_gpio_init(void)
138 {
139 	return platform_driver_register(&rc5t583_gpio_driver);
140 }
141 subsys_initcall(rc5t583_gpio_init);
142