1 // SPDX-License-Identifier: GPL-2.0
2 /* Author: Dan Scally <djrscally@gmail.com> */
4 #include <linux/acpi.h>
5 #include <linux/bitfield.h>
6 #include <linux/device.h>
7 #include <linux/gpio/consumer.h>
8 #include <linux/gpio/machine.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/overflow.h>
13 #include <linux/platform_device.h>
14 #include <linux/string_choices.h>
15 #include <linux/uuid.h>
20 * 79234640-9e10-4fea-a5c1-b5aa8b19756f
21 * This _DSM GUID returns information about the GPIO lines mapped to a
22 * discrete INT3472 device. Function number 1 returns a count of the GPIO
23 * lines that are mapped. Subsequent functions return 32 bit ints encoding
24 * information about the GPIO line, including its purpose.
26 static const guid_t int3472_gpio_guid
=
27 GUID_INIT(0x79234640, 0x9e10, 0x4fea,
28 0xa5, 0xc1, 0xb5, 0xaa, 0x8b, 0x19, 0x75, 0x6f);
30 #define INT3472_GPIO_DSM_TYPE GENMASK(7, 0)
31 #define INT3472_GPIO_DSM_PIN GENMASK(15, 8)
32 #define INT3472_GPIO_DSM_SENSOR_ON_VAL GENMASK(31, 24)
35 * 822ace8f-2814-4174-a56b-5f029fe079ee
36 * This _DSM GUID returns a string from the sensor device, which acts as a
39 static const guid_t cio2_sensor_module_guid
=
40 GUID_INIT(0x822ace8f, 0x2814, 0x4174,
41 0xa5, 0x6b, 0x5f, 0x02, 0x9f, 0xe0, 0x79, 0xee);
43 static void skl_int3472_log_sensor_module_name(struct int3472_discrete_device
*int3472
)
45 union acpi_object
*obj
;
47 obj
= acpi_evaluate_dsm_typed(int3472
->sensor
->handle
,
48 &cio2_sensor_module_guid
, 0x00,
49 0x01, NULL
, ACPI_TYPE_STRING
);
51 dev_dbg(int3472
->dev
, "Sensor module id: '%s'\n", obj
->string
.pointer
);
56 static int skl_int3472_fill_gpiod_lookup(struct gpiod_lookup
*table_entry
,
57 struct acpi_resource_gpio
*agpio
,
58 const char *func
, u32 polarity
)
60 char *path
= agpio
->resource_source
.string_ptr
;
61 struct acpi_device
*adev
;
65 status
= acpi_get_handle(NULL
, path
, &handle
);
66 if (ACPI_FAILURE(status
))
69 adev
= acpi_fetch_acpi_dev(handle
);
73 *table_entry
= GPIO_LOOKUP(acpi_dev_name(adev
), agpio
->pin_table
[0], func
, polarity
);
78 static int skl_int3472_map_gpio_to_sensor(struct int3472_discrete_device
*int3472
,
79 struct acpi_resource_gpio
*agpio
,
80 const char *func
, u32 polarity
)
84 if (int3472
->n_sensor_gpios
>= INT3472_MAX_SENSOR_GPIOS
) {
85 dev_warn(int3472
->dev
, "Too many GPIOs mapped\n");
89 ret
= skl_int3472_fill_gpiod_lookup(&int3472
->gpios
.table
[int3472
->n_sensor_gpios
],
90 agpio
, func
, polarity
);
94 int3472
->n_sensor_gpios
++;
99 /* This should *really* only be used when there's no other way... */
100 static struct gpio_desc
*
101 skl_int3472_gpiod_get_from_temp_lookup(struct int3472_discrete_device
*int3472
,
102 struct acpi_resource_gpio
*agpio
,
103 const char *func
, u32 polarity
)
105 struct gpio_desc
*desc
;
108 struct gpiod_lookup_table
*lookup
__free(kfree
) =
109 kzalloc(struct_size(lookup
, table
, 2), GFP_KERNEL
);
111 return ERR_PTR(-ENOMEM
);
113 lookup
->dev_id
= dev_name(int3472
->dev
);
114 ret
= skl_int3472_fill_gpiod_lookup(&lookup
->table
[0], agpio
, func
, polarity
);
118 gpiod_add_lookup_table(lookup
);
119 desc
= devm_gpiod_get(int3472
->dev
, func
, GPIOD_OUT_LOW
);
120 gpiod_remove_lookup_table(lookup
);
125 static void int3472_get_func_and_polarity(u8 type
, const char **func
, u32
*polarity
)
128 case INT3472_GPIO_TYPE_RESET
:
130 *polarity
= GPIO_ACTIVE_LOW
;
132 case INT3472_GPIO_TYPE_POWERDOWN
:
134 *polarity
= GPIO_ACTIVE_LOW
;
136 case INT3472_GPIO_TYPE_CLK_ENABLE
:
137 *func
= "clk-enable";
138 *polarity
= GPIO_ACTIVE_HIGH
;
140 case INT3472_GPIO_TYPE_PRIVACY_LED
:
141 *func
= "privacy-led";
142 *polarity
= GPIO_ACTIVE_HIGH
;
144 case INT3472_GPIO_TYPE_POWER_ENABLE
:
145 *func
= "power-enable";
146 *polarity
= GPIO_ACTIVE_HIGH
;
150 *polarity
= GPIO_ACTIVE_HIGH
;
156 * skl_int3472_handle_gpio_resources: Map PMIC resources to consuming sensor
157 * @ares: A pointer to a &struct acpi_resource
158 * @data: A pointer to a &struct int3472_discrete_device
160 * This function handles GPIO resources that are against an INT3472
161 * ACPI device, by checking the value of the corresponding _DSM entry.
162 * This will return a 32bit int, where the lowest byte represents the
163 * function of the GPIO pin:
171 * There are some known platform specific quirks where that does not quite
172 * hold up; for example where a pin with type 0x01 (Power down) is mapped to
173 * a sensor pin that performs a reset function or entries in _CRS and _DSM that
174 * do not actually correspond to a physical connection. These will be handled
175 * by the mapping sub-functions.
177 * GPIOs will either be mapped directly to the sensor device or else used
178 * to create clocks and regulators via the usual frameworks.
181 * * 1 - To continue the loop
182 * * 0 - When all resources found are handled properly.
183 * * -EINVAL - If the resource is not a GPIO IO resource
184 * * -ENODEV - If the resource has no corresponding _DSM entry
185 * * -Other - Errors propagated from one of the sub-functions.
187 static int skl_int3472_handle_gpio_resources(struct acpi_resource
*ares
,
190 struct int3472_discrete_device
*int3472
= data
;
191 struct acpi_resource_gpio
*agpio
;
192 u8 active_value
, pin
, type
;
193 union acpi_object
*obj
;
194 struct gpio_desc
*gpio
;
200 if (!acpi_gpio_get_io_resource(ares
, &agpio
))
204 * ngpios + 2 because the index of this _DSM function is 1-based and
205 * the first function is just a count.
207 obj
= acpi_evaluate_dsm_typed(int3472
->adev
->handle
,
208 &int3472_gpio_guid
, 0x00,
210 NULL
, ACPI_TYPE_INTEGER
);
213 dev_warn(int3472
->dev
, "No _DSM entry for GPIO pin %u\n",
214 agpio
->pin_table
[0]);
218 type
= FIELD_GET(INT3472_GPIO_DSM_TYPE
, obj
->integer
.value
);
220 int3472_get_func_and_polarity(type
, &func
, &polarity
);
222 pin
= FIELD_GET(INT3472_GPIO_DSM_PIN
, obj
->integer
.value
);
223 if (pin
!= agpio
->pin_table
[0])
224 dev_warn(int3472
->dev
, "%s %s pin number mismatch _DSM %d resource %d\n",
225 func
, agpio
->resource_source
.string_ptr
, pin
,
226 agpio
->pin_table
[0]);
228 active_value
= FIELD_GET(INT3472_GPIO_DSM_SENSOR_ON_VAL
, obj
->integer
.value
);
230 polarity
^= GPIO_ACTIVE_LOW
;
232 dev_dbg(int3472
->dev
, "%s %s pin %d active-%s\n", func
,
233 agpio
->resource_source
.string_ptr
, agpio
->pin_table
[0],
234 str_high_low(polarity
== GPIO_ACTIVE_HIGH
));
237 case INT3472_GPIO_TYPE_RESET
:
238 case INT3472_GPIO_TYPE_POWERDOWN
:
239 ret
= skl_int3472_map_gpio_to_sensor(int3472
, agpio
, func
, polarity
);
241 err_msg
= "Failed to map GPIO pin to sensor\n";
244 case INT3472_GPIO_TYPE_CLK_ENABLE
:
245 case INT3472_GPIO_TYPE_PRIVACY_LED
:
246 case INT3472_GPIO_TYPE_POWER_ENABLE
:
247 gpio
= skl_int3472_gpiod_get_from_temp_lookup(int3472
, agpio
, func
, polarity
);
250 err_msg
= "Failed to get GPIO\n";
255 case INT3472_GPIO_TYPE_CLK_ENABLE
:
256 ret
= skl_int3472_register_gpio_clock(int3472
, gpio
);
258 err_msg
= "Failed to register clock\n";
261 case INT3472_GPIO_TYPE_PRIVACY_LED
:
262 ret
= skl_int3472_register_pled(int3472
, gpio
);
264 err_msg
= "Failed to register LED\n";
267 case INT3472_GPIO_TYPE_POWER_ENABLE
:
268 ret
= skl_int3472_register_regulator(int3472
, gpio
);
270 err_msg
= "Failed to map regulator to sensor\n";
273 default: /* Never reached */
279 dev_warn(int3472
->dev
,
280 "GPIO type 0x%02x unknown; the sensor may not work\n",
290 return dev_err_probe(int3472
->dev
, ret
, err_msg
);
295 static int skl_int3472_parse_crs(struct int3472_discrete_device
*int3472
)
297 LIST_HEAD(resource_list
);
300 skl_int3472_log_sensor_module_name(int3472
);
302 ret
= acpi_dev_get_resources(int3472
->adev
, &resource_list
,
303 skl_int3472_handle_gpio_resources
,
308 acpi_dev_free_resource_list(&resource_list
);
310 /* Register _DSM based clock (no-op if a GPIO clock was already registered) */
311 ret
= skl_int3472_register_dsm_clock(int3472
);
315 int3472
->gpios
.dev_id
= int3472
->sensor_name
;
316 gpiod_add_lookup_table(&int3472
->gpios
);
321 static void skl_int3472_discrete_remove(struct platform_device
*pdev
)
323 struct int3472_discrete_device
*int3472
= platform_get_drvdata(pdev
);
325 gpiod_remove_lookup_table(&int3472
->gpios
);
327 skl_int3472_unregister_clock(int3472
);
328 skl_int3472_unregister_pled(int3472
);
329 skl_int3472_unregister_regulator(int3472
);
332 static int skl_int3472_discrete_probe(struct platform_device
*pdev
)
334 struct acpi_device
*adev
= ACPI_COMPANION(&pdev
->dev
);
335 struct int3472_discrete_device
*int3472
;
336 struct int3472_cldb cldb
;
339 ret
= skl_int3472_fill_cldb(adev
, &cldb
);
341 dev_err(&pdev
->dev
, "Couldn't fill CLDB structure\n");
345 if (cldb
.control_logic_type
!= 1) {
346 dev_err(&pdev
->dev
, "Unsupported control logic type %u\n",
347 cldb
.control_logic_type
);
351 /* Max num GPIOs we've seen plus a terminator */
352 int3472
= devm_kzalloc(&pdev
->dev
, struct_size(int3472
, gpios
.table
,
353 INT3472_MAX_SENSOR_GPIOS
+ 1), GFP_KERNEL
);
357 int3472
->adev
= adev
;
358 int3472
->dev
= &pdev
->dev
;
359 platform_set_drvdata(pdev
, int3472
);
360 int3472
->clock
.imgclk_index
= cldb
.clock_source
;
362 ret
= skl_int3472_get_sensor_adev_and_name(&pdev
->dev
, &int3472
->sensor
,
363 &int3472
->sensor_name
);
368 * Initialising this list means we can call gpiod_remove_lookup_table()
369 * in failure paths without issue.
371 INIT_LIST_HEAD(&int3472
->gpios
.list
);
373 ret
= skl_int3472_parse_crs(int3472
);
375 skl_int3472_discrete_remove(pdev
);
379 acpi_dev_clear_dependencies(adev
);
383 static const struct acpi_device_id int3472_device_id
[] = {
387 MODULE_DEVICE_TABLE(acpi
, int3472_device_id
);
389 static struct platform_driver int3472_discrete
= {
391 .name
= "int3472-discrete",
392 .acpi_match_table
= int3472_device_id
,
394 .probe
= skl_int3472_discrete_probe
,
395 .remove
= skl_int3472_discrete_remove
,
397 module_platform_driver(int3472_discrete
);
399 MODULE_DESCRIPTION("Intel SkyLake INT3472 ACPI Discrete Device Driver");
400 MODULE_AUTHOR("Daniel Scally <djrscally@gmail.com>");
401 MODULE_LICENSE("GPL v2");