1 // SPDX-License-Identifier: GPL-2.0
3 * Intel PCH pinctrl/GPIO driver
5 * Copyright (C) 2021-2023, Intel Corporation
6 * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
9 #include <linux/mod_devicetable.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.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
;
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
;
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
);
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
;
55 pins
->npins
= base
+ size
;
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
;
70 ret
= fwnode_property_read_string(child
, "intc-gpio-group-name", &name
);
74 ret
= fwnode_property_read_u32(child
, "intc-gpio-pad-count", &size
);
78 ret
= intel_platform_pinctrl_prepare_pins(dev
, base
, name
, size
, pins
);
84 gpp
->gpio_base
= INTEL_GPIO_BASE_MATCH
;
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
;
99 ret
= device_property_read_u32(dev
, "intc-gpio-pad-ownership-offset", &offset
);
102 community
->padown_offset
= offset
;
104 ret
= device_property_read_u32(dev
, "intc-gpio-pad-configuration-lock-offset", &offset
);
107 community
->padcfglock_offset
= offset
;
109 ret
= device_property_read_u32(dev
, "intc-gpio-host-software-pad-ownership-offset", &offset
);
112 community
->hostown_offset
= offset
;
114 ret
= device_property_read_u32(dev
, "intc-gpio-gpi-interrupt-status-offset", &offset
);
117 community
->is_offset
= offset
;
119 ret
= device_property_read_u32(dev
, "intc-gpio-gpi-interrupt-enable-offset", &offset
);
122 community
->ie_offset
= offset
;
124 ngpps
= device_get_child_node_count(dev
);
128 gpps
= devm_kcalloc(dev
, ngpps
, sizeof(*gpps
), GFP_KERNEL
);
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
);
145 community
->ngpps
= ngpps
;
146 community
->gpps
= gpps
;
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
;
160 /* Version 1.0 of the specification assumes only a single community per device node */
162 communities
= devm_kcalloc(dev
, ncommunities
, sizeof(*communities
), GFP_KERNEL
);
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
);
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
;
188 static int intel_platform_pinctrl_probe(struct platform_device
*pdev
)
190 struct intel_pinctrl_soc_data
*data
;
191 struct device
*dev
= &pdev
->dev
;
194 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
198 ret
= intel_platform_pinctrl_prepare_soc_data(dev
, data
);
202 return intel_pinctrl_probe(pdev
, data
);
205 static const struct acpi_device_id intel_platform_pinctrl_acpi_match
[] = {
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
,
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");