Merge tag 'clk-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / drivers / pinctrl / intel / pinctrl-intel-platform.c
blobdd5dbede0f591a5403673239ddf12554261a26a7
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Intel PCH pinctrl/GPIO driver
5 * Copyright (C) 2021-2023, Intel Corporation
6 * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
7 */
9 #include <linux/mod_devicetable.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/pm.h>
13 #include <linux/property.h>
14 #include <linux/string_helpers.h>
16 #include <linux/pinctrl/pinctrl.h>
18 #include "pinctrl-intel.h"
20 struct intel_platform_pins {
21 struct pinctrl_pin_desc *pins;
22 size_t npins;
25 static int intel_platform_pinctrl_prepare_pins(struct device *dev, size_t base,
26 const char *name, u32 size,
27 struct intel_platform_pins *pins)
29 struct pinctrl_pin_desc *descs;
30 char **pin_names;
31 unsigned int i;
33 pin_names = devm_kasprintf_strarray(dev, name, size);
34 if (IS_ERR(pin_names))
35 return PTR_ERR(pin_names);
37 descs = devm_krealloc_array(dev, pins->pins, base + size, sizeof(*descs), GFP_KERNEL);
38 if (!descs)
39 return -ENOMEM;
41 for (i = 0; i < size; i++) {
42 unsigned int pin_number = base + i;
43 char *pin_name = pin_names[i];
44 struct pinctrl_pin_desc *desc;
46 /* Unify delimiter for pin name */
47 strreplace(pin_name, '-', '_');
49 desc = &descs[pin_number];
50 desc->number = pin_number;
51 desc->name = pin_name;
54 pins->pins = descs;
55 pins->npins = base + size;
57 return 0;
60 static int intel_platform_pinctrl_prepare_group(struct device *dev,
61 struct fwnode_handle *child,
62 struct intel_padgroup *gpp,
63 struct intel_platform_pins *pins)
65 size_t base = pins->npins;
66 const char *name;
67 u32 size;
68 int ret;
70 ret = fwnode_property_read_string(child, "intc-gpio-group-name", &name);
71 if (ret)
72 return ret;
74 ret = fwnode_property_read_u32(child, "intc-gpio-pad-count", &size);
75 if (ret)
76 return ret;
78 ret = intel_platform_pinctrl_prepare_pins(dev, base, name, size, pins);
79 if (ret)
80 return ret;
82 gpp->base = base;
83 gpp->size = size;
84 gpp->gpio_base = INTEL_GPIO_BASE_MATCH;
86 return 0;
89 static int intel_platform_pinctrl_prepare_community(struct device *dev,
90 struct intel_community *community,
91 struct intel_platform_pins *pins)
93 struct intel_padgroup *gpps;
94 unsigned int group;
95 size_t ngpps;
96 u32 offset;
97 int ret;
99 ret = device_property_read_u32(dev, "intc-gpio-pad-ownership-offset", &offset);
100 if (ret)
101 return ret;
102 community->padown_offset = offset;
104 ret = device_property_read_u32(dev, "intc-gpio-pad-configuration-lock-offset", &offset);
105 if (ret)
106 return ret;
107 community->padcfglock_offset = offset;
109 ret = device_property_read_u32(dev, "intc-gpio-host-software-pad-ownership-offset", &offset);
110 if (ret)
111 return ret;
112 community->hostown_offset = offset;
114 ret = device_property_read_u32(dev, "intc-gpio-gpi-interrupt-status-offset", &offset);
115 if (ret)
116 return ret;
117 community->is_offset = offset;
119 ret = device_property_read_u32(dev, "intc-gpio-gpi-interrupt-enable-offset", &offset);
120 if (ret)
121 return ret;
122 community->ie_offset = offset;
124 ngpps = device_get_child_node_count(dev);
125 if (!ngpps)
126 return -ENODEV;
128 gpps = devm_kcalloc(dev, ngpps, sizeof(*gpps), GFP_KERNEL);
129 if (!gpps)
130 return -ENOMEM;
132 group = 0;
133 device_for_each_child_node_scoped(dev, child) {
134 struct intel_padgroup *gpp = &gpps[group];
136 gpp->reg_num = group;
138 ret = intel_platform_pinctrl_prepare_group(dev, child, gpp, pins);
139 if (ret)
140 return ret;
142 group++;
145 community->ngpps = ngpps;
146 community->gpps = gpps;
148 return 0;
151 static int intel_platform_pinctrl_prepare_soc_data(struct device *dev,
152 struct intel_pinctrl_soc_data *data)
154 struct intel_platform_pins pins = {};
155 struct intel_community *communities;
156 size_t ncommunities;
157 unsigned int i;
158 int ret;
160 /* Version 1.0 of the specification assumes only a single community per device node */
161 ncommunities = 1;
162 communities = devm_kcalloc(dev, ncommunities, sizeof(*communities), GFP_KERNEL);
163 if (!communities)
164 return -ENOMEM;
166 for (i = 0; i < ncommunities; i++) {
167 struct intel_community *community = &communities[i];
169 community->barno = i;
170 community->pin_base = pins.npins;
172 ret = intel_platform_pinctrl_prepare_community(dev, community, &pins);
173 if (ret)
174 return ret;
176 community->npins = pins.npins - community->pin_base;
179 data->ncommunities = ncommunities;
180 data->communities = communities;
182 data->npins = pins.npins;
183 data->pins = pins.pins;
185 return 0;
188 static int intel_platform_pinctrl_probe(struct platform_device *pdev)
190 struct intel_pinctrl_soc_data *data;
191 struct device *dev = &pdev->dev;
192 int ret;
194 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
195 if (!data)
196 return -ENOMEM;
198 ret = intel_platform_pinctrl_prepare_soc_data(dev, data);
199 if (ret)
200 return ret;
202 return intel_pinctrl_probe(pdev, data);
205 static const struct acpi_device_id intel_platform_pinctrl_acpi_match[] = {
206 { "INTC105F" },
209 MODULE_DEVICE_TABLE(acpi, intel_platform_pinctrl_acpi_match);
211 static struct platform_driver intel_platform_pinctrl_driver = {
212 .probe = intel_platform_pinctrl_probe,
213 .driver = {
214 .name = "intel-pinctrl",
215 .acpi_match_table = intel_platform_pinctrl_acpi_match,
216 .pm = pm_sleep_ptr(&intel_pinctrl_pm_ops),
219 module_platform_driver(intel_platform_pinctrl_driver);
221 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
222 MODULE_DESCRIPTION("Intel PCH pinctrl/GPIO driver");
223 MODULE_LICENSE("GPL v2");
224 MODULE_IMPORT_NS("PINCTRL_INTEL");