]> Git Repo - linux.git/blob - drivers/platform/x86/intel/int3472/discrete.c
drm/i915: Use REG_BIT() & co. for AUX CH registers
[linux.git] / drivers / platform / x86 / intel / int3472 / discrete.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Author: Dan Scally <[email protected]> */
3
4 #include <linux/acpi.h>
5 #include <linux/device.h>
6 #include <linux/gpio/consumer.h>
7 #include <linux/gpio/machine.h>
8 #include <linux/i2c.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/overflow.h>
12 #include <linux/platform_device.h>
13 #include <linux/uuid.h>
14
15 #include "common.h"
16
17 /*
18  * 79234640-9e10-4fea-a5c1-b5aa8b19756f
19  * This _DSM GUID returns information about the GPIO lines mapped to a
20  * discrete INT3472 device. Function number 1 returns a count of the GPIO
21  * lines that are mapped. Subsequent functions return 32 bit ints encoding
22  * information about the GPIO line, including its purpose.
23  */
24 static const guid_t int3472_gpio_guid =
25         GUID_INIT(0x79234640, 0x9e10, 0x4fea,
26                   0xa5, 0xc1, 0xb5, 0xaa, 0x8b, 0x19, 0x75, 0x6f);
27
28 /*
29  * 822ace8f-2814-4174-a56b-5f029fe079ee
30  * This _DSM GUID returns a string from the sensor device, which acts as a
31  * module identifier.
32  */
33 static const guid_t cio2_sensor_module_guid =
34         GUID_INIT(0x822ace8f, 0x2814, 0x4174,
35                   0xa5, 0x6b, 0x5f, 0x02, 0x9f, 0xe0, 0x79, 0xee);
36
37 /*
38  * Here follows platform specific mapping information that we can pass to
39  * the functions mapping resources to the sensors. Where the sensors have
40  * a power enable pin defined in DSDT we need to provide a supply name so
41  * the sensor drivers can find the regulator. The device name will be derived
42  * from the sensor's ACPI device within the code. Optionally, we can provide a
43  * NULL terminated array of function name mappings to deal with any platform
44  * specific deviations from the documented behaviour of GPIOs.
45  *
46  * Map a GPIO function name to NULL to prevent the driver from mapping that
47  * GPIO at all.
48  */
49
50 static const struct int3472_gpio_function_remap ov2680_gpio_function_remaps[] = {
51         { "reset", NULL },
52         { "powerdown", "reset" },
53         { }
54 };
55
56 static const struct int3472_sensor_config int3472_sensor_configs[] = {
57         /* Lenovo Miix 510-12ISK - OV2680, Front */
58         { "GNDF140809R", { 0 }, ov2680_gpio_function_remaps },
59         /* Lenovo Miix 510-12ISK - OV5648, Rear */
60         { "GEFF150023R", REGULATOR_SUPPLY("avdd", NULL), NULL },
61         /* Surface Go 1&2 - OV5693, Front */
62         { "YHCU", REGULATOR_SUPPLY("avdd", NULL), NULL },
63 };
64
65 static const struct int3472_sensor_config *
66 skl_int3472_get_sensor_module_config(struct int3472_discrete_device *int3472)
67 {
68         union acpi_object *obj;
69         unsigned int i;
70
71         obj = acpi_evaluate_dsm_typed(int3472->sensor->handle,
72                                       &cio2_sensor_module_guid, 0x00,
73                                       0x01, NULL, ACPI_TYPE_STRING);
74
75         if (!obj) {
76                 dev_err(int3472->dev,
77                         "Failed to get sensor module string from _DSM\n");
78                 return ERR_PTR(-ENODEV);
79         }
80
81         for (i = 0; i < ARRAY_SIZE(int3472_sensor_configs); i++) {
82                 if (!strcmp(int3472_sensor_configs[i].sensor_module_name,
83                             obj->string.pointer))
84                         break;
85         }
86
87         ACPI_FREE(obj);
88
89         if (i >= ARRAY_SIZE(int3472_sensor_configs))
90                 return ERR_PTR(-EINVAL);
91
92         return &int3472_sensor_configs[i];
93 }
94
95 static int skl_int3472_map_gpio_to_sensor(struct int3472_discrete_device *int3472,
96                                           struct acpi_resource_gpio *agpio,
97                                           const char *func, u32 polarity)
98 {
99         const struct int3472_sensor_config *sensor_config;
100         char *path = agpio->resource_source.string_ptr;
101         struct gpiod_lookup *table_entry;
102         struct acpi_device *adev;
103         acpi_handle handle;
104         acpi_status status;
105
106         if (int3472->n_sensor_gpios >= INT3472_MAX_SENSOR_GPIOS) {
107                 dev_warn(int3472->dev, "Too many GPIOs mapped\n");
108                 return -EINVAL;
109         }
110
111         sensor_config = int3472->sensor_config;
112         if (!IS_ERR(sensor_config) && sensor_config->function_maps) {
113                 const struct int3472_gpio_function_remap *remap;
114
115                 for (remap = sensor_config->function_maps; remap->documented; remap++) {
116                         if (!strcmp(func, remap->documented)) {
117                                 func = remap->actual;
118                                 break;
119                         }
120                 }
121         }
122
123         /* Functions mapped to NULL should not be mapped to the sensor */
124         if (!func)
125                 return 0;
126
127         status = acpi_get_handle(NULL, path, &handle);
128         if (ACPI_FAILURE(status))
129                 return -EINVAL;
130
131         adev = acpi_fetch_acpi_dev(handle);
132         if (!adev)
133                 return -ENODEV;
134
135         table_entry = &int3472->gpios.table[int3472->n_sensor_gpios];
136         table_entry->key = acpi_dev_name(adev);
137         table_entry->chip_hwnum = agpio->pin_table[0];
138         table_entry->con_id = func;
139         table_entry->idx = 0;
140         table_entry->flags = polarity;
141
142         int3472->n_sensor_gpios++;
143
144         return 0;
145 }
146
147 static void int3472_get_func_and_polarity(u8 type, const char **func, u32 *polarity)
148 {
149         switch (type) {
150         case INT3472_GPIO_TYPE_RESET:
151                 *func = "reset";
152                 *polarity = GPIO_ACTIVE_LOW;
153                 break;
154         case INT3472_GPIO_TYPE_POWERDOWN:
155                 *func = "powerdown";
156                 *polarity = GPIO_ACTIVE_LOW;
157                 break;
158         case INT3472_GPIO_TYPE_CLK_ENABLE:
159                 *func = "clk-enable";
160                 *polarity = GPIO_ACTIVE_HIGH;
161                 break;
162         case INT3472_GPIO_TYPE_PRIVACY_LED:
163                 *func = "privacy-led";
164                 *polarity = GPIO_ACTIVE_HIGH;
165                 break;
166         case INT3472_GPIO_TYPE_POWER_ENABLE:
167                 *func = "power-enable";
168                 *polarity = GPIO_ACTIVE_HIGH;
169                 break;
170         default:
171                 *func = "unknown";
172                 *polarity = GPIO_ACTIVE_HIGH;
173                 break;
174         }
175 }
176
177 /**
178  * skl_int3472_handle_gpio_resources: Map PMIC resources to consuming sensor
179  * @ares: A pointer to a &struct acpi_resource
180  * @data: A pointer to a &struct int3472_discrete_device
181  *
182  * This function handles GPIO resources that are against an INT3472
183  * ACPI device, by checking the value of the corresponding _DSM entry.
184  * This will return a 32bit int, where the lowest byte represents the
185  * function of the GPIO pin:
186  *
187  * 0x00 Reset
188  * 0x01 Power down
189  * 0x0b Power enable
190  * 0x0c Clock enable
191  * 0x0d Privacy LED
192  *
193  * There are some known platform specific quirks where that does not quite
194  * hold up; for example where a pin with type 0x01 (Power down) is mapped to
195  * a sensor pin that performs a reset function or entries in _CRS and _DSM that
196  * do not actually correspond to a physical connection. These will be handled
197  * by the mapping sub-functions.
198  *
199  * GPIOs will either be mapped directly to the sensor device or else used
200  * to create clocks and regulators via the usual frameworks.
201  *
202  * Return:
203  * * 1          - To continue the loop
204  * * 0          - When all resources found are handled properly.
205  * * -EINVAL    - If the resource is not a GPIO IO resource
206  * * -ENODEV    - If the resource has no corresponding _DSM entry
207  * * -Other     - Errors propagated from one of the sub-functions.
208  */
209 static int skl_int3472_handle_gpio_resources(struct acpi_resource *ares,
210                                              void *data)
211 {
212         struct int3472_discrete_device *int3472 = data;
213         struct acpi_resource_gpio *agpio;
214         union acpi_object *obj;
215         u8 active_value, type;
216         const char *err_msg;
217         const char *func;
218         u32 polarity;
219         int ret;
220
221         if (!acpi_gpio_get_io_resource(ares, &agpio))
222                 return 1;
223
224         /*
225          * ngpios + 2 because the index of this _DSM function is 1-based and
226          * the first function is just a count.
227          */
228         obj = acpi_evaluate_dsm_typed(int3472->adev->handle,
229                                       &int3472_gpio_guid, 0x00,
230                                       int3472->ngpios + 2,
231                                       NULL, ACPI_TYPE_INTEGER);
232
233         if (!obj) {
234                 dev_warn(int3472->dev, "No _DSM entry for GPIO pin %u\n",
235                          agpio->pin_table[0]);
236                 return 1;
237         }
238
239         type = obj->integer.value & 0xff;
240
241         int3472_get_func_and_polarity(type, &func, &polarity);
242
243         /* If bits 31-24 of the _DSM entry are all 0 then the signal is inverted */
244         active_value = obj->integer.value >> 24;
245         if (!active_value)
246                 polarity ^= GPIO_ACTIVE_LOW;
247
248         dev_dbg(int3472->dev, "%s %s pin %d active-%s\n", func,
249                 agpio->resource_source.string_ptr, agpio->pin_table[0],
250                 (polarity == GPIO_ACTIVE_HIGH) ? "high" : "low");
251
252         switch (type) {
253         case INT3472_GPIO_TYPE_RESET:
254         case INT3472_GPIO_TYPE_POWERDOWN:
255                 ret = skl_int3472_map_gpio_to_sensor(int3472, agpio, func, polarity);
256                 if (ret)
257                         err_msg = "Failed to map GPIO pin to sensor\n";
258
259                 break;
260         case INT3472_GPIO_TYPE_CLK_ENABLE:
261                 ret = skl_int3472_register_clock(int3472, agpio, polarity);
262                 if (ret)
263                         err_msg = "Failed to register clock\n";
264
265                 break;
266         case INT3472_GPIO_TYPE_PRIVACY_LED:
267                 ret = skl_int3472_register_pled(int3472, agpio, polarity);
268                 if (ret)
269                         err_msg = "Failed to register LED\n";
270
271                 break;
272         case INT3472_GPIO_TYPE_POWER_ENABLE:
273                 ret = skl_int3472_register_regulator(int3472, agpio);
274                 if (ret)
275                         err_msg = "Failed to map regulator to sensor\n";
276
277                 break;
278         default:
279                 dev_warn(int3472->dev,
280                          "GPIO type 0x%02x unknown; the sensor may not work\n",
281                          type);
282                 ret = 1;
283                 break;
284         }
285
286         int3472->ngpios++;
287         ACPI_FREE(obj);
288
289         if (ret < 0)
290                 return dev_err_probe(int3472->dev, ret, err_msg);
291
292         return ret;
293 }
294
295 static int skl_int3472_parse_crs(struct int3472_discrete_device *int3472)
296 {
297         LIST_HEAD(resource_list);
298         int ret;
299
300         /*
301          * No error check, because not having a sensor config is not necessarily
302          * a failure mode.
303          */
304         int3472->sensor_config = skl_int3472_get_sensor_module_config(int3472);
305
306         ret = acpi_dev_get_resources(int3472->adev, &resource_list,
307                                      skl_int3472_handle_gpio_resources,
308                                      int3472);
309         if (ret < 0)
310                 return ret;
311
312         acpi_dev_free_resource_list(&resource_list);
313
314         int3472->gpios.dev_id = int3472->sensor_name;
315         gpiod_add_lookup_table(&int3472->gpios);
316
317         return 0;
318 }
319
320 static int skl_int3472_discrete_remove(struct platform_device *pdev)
321 {
322         struct int3472_discrete_device *int3472 = platform_get_drvdata(pdev);
323
324         gpiod_remove_lookup_table(&int3472->gpios);
325
326         skl_int3472_unregister_clock(int3472);
327         skl_int3472_unregister_pled(int3472);
328         skl_int3472_unregister_regulator(int3472);
329
330         return 0;
331 }
332
333 static int skl_int3472_discrete_probe(struct platform_device *pdev)
334 {
335         struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
336         struct int3472_discrete_device *int3472;
337         struct int3472_cldb cldb;
338         int ret;
339
340         ret = skl_int3472_fill_cldb(adev, &cldb);
341         if (ret) {
342                 dev_err(&pdev->dev, "Couldn't fill CLDB structure\n");
343                 return ret;
344         }
345
346         if (cldb.control_logic_type != 1) {
347                 dev_err(&pdev->dev, "Unsupported control logic type %u\n",
348                         cldb.control_logic_type);
349                 return -EINVAL;
350         }
351
352         /* Max num GPIOs we've seen plus a terminator */
353         int3472 = devm_kzalloc(&pdev->dev, struct_size(int3472, gpios.table,
354                                INT3472_MAX_SENSOR_GPIOS + 1), GFP_KERNEL);
355         if (!int3472)
356                 return -ENOMEM;
357
358         int3472->adev = adev;
359         int3472->dev = &pdev->dev;
360         platform_set_drvdata(pdev, int3472);
361
362         ret = skl_int3472_get_sensor_adev_and_name(&pdev->dev, &int3472->sensor,
363                                                    &int3472->sensor_name);
364         if (ret)
365                 return ret;
366
367         /*
368          * Initialising this list means we can call gpiod_remove_lookup_table()
369          * in failure paths without issue.
370          */
371         INIT_LIST_HEAD(&int3472->gpios.list);
372
373         ret = skl_int3472_parse_crs(int3472);
374         if (ret) {
375                 skl_int3472_discrete_remove(pdev);
376                 return ret;
377         }
378
379         acpi_dev_clear_dependencies(adev);
380         return 0;
381 }
382
383 static const struct acpi_device_id int3472_device_id[] = {
384         { "INT3472", 0 },
385         { }
386 };
387 MODULE_DEVICE_TABLE(acpi, int3472_device_id);
388
389 static struct platform_driver int3472_discrete = {
390         .driver = {
391                 .name = "int3472-discrete",
392                 .acpi_match_table = int3472_device_id,
393         },
394         .probe = skl_int3472_discrete_probe,
395         .remove = skl_int3472_discrete_remove,
396 };
397 module_platform_driver(int3472_discrete);
398
399 MODULE_DESCRIPTION("Intel SkyLake INT3472 ACPI Discrete Device Driver");
400 MODULE_AUTHOR("Daniel Scally <[email protected]>");
401 MODULE_LICENSE("GPL v2");
This page took 0.062265 seconds and 4 git commands to generate.