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