1 // SPDX-License-Identifier: GPL-2.0
2 /* Author: Dan Scally <djrscally@gmail.com> */
3
4 #include <linux/acpi.h>
5 #include <linux/array_size.h>
6 #include <linux/bitfield.h>
7 #include <linux/device.h>
8 #include <linux/dmi.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/gpio/machine.h>
11 #include <linux/i2c.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/overflow.h>
15 #include <linux/platform_data/x86/int3472.h>
16 #include <linux/platform_device.h>
17 #include <linux/string_choices.h>
18 #include <linux/uuid.h>
19
20 /*
21 * 79234640-9e10-4fea-a5c1-b5aa8b19756f
22 * This _DSM GUID returns information about the GPIO lines mapped to a
23 * discrete INT3472 device. Function number 1 returns a count of the GPIO
24 * lines that are mapped. Subsequent functions return 32 bit ints encoding
25 * information about the GPIO line, including its purpose.
26 */
27 static const guid_t int3472_gpio_guid =
28 GUID_INIT(0x79234640, 0x9e10, 0x4fea,
29 0xa5, 0xc1, 0xb5, 0xaa, 0x8b, 0x19, 0x75, 0x6f);
30
31 #define INT3472_GPIO_DSM_TYPE GENMASK(7, 0)
32 #define INT3472_GPIO_DSM_PIN GENMASK(15, 8)
33 #define INT3472_GPIO_DSM_SENSOR_ON_VAL GENMASK(31, 24)
34
35 /*
36 * 822ace8f-2814-4174-a56b-5f029fe079ee
37 * This _DSM GUID returns a string from the sensor device, which acts as a
38 * module identifier.
39 */
40 static const guid_t cio2_sensor_module_guid =
41 GUID_INIT(0x822ace8f, 0x2814, 0x4174,
42 0xa5, 0x6b, 0x5f, 0x02, 0x9f, 0xe0, 0x79, 0xee);
43
skl_int3472_log_sensor_module_name(struct int3472_discrete_device * int3472)44 static void skl_int3472_log_sensor_module_name(struct int3472_discrete_device *int3472)
45 {
46 union acpi_object *obj;
47
48 obj = acpi_evaluate_dsm_typed(int3472->sensor->handle,
49 &cio2_sensor_module_guid, 0x00,
50 0x01, NULL, ACPI_TYPE_STRING);
51 if (obj) {
52 dev_dbg(int3472->dev, "Sensor module id: '%s'\n", obj->string.pointer);
53 ACPI_FREE(obj);
54 }
55 }
56
skl_int3472_fill_gpiod_lookup(struct gpiod_lookup * table_entry,struct acpi_resource_gpio * agpio,const char * con_id,unsigned long gpio_flags)57 static int skl_int3472_fill_gpiod_lookup(struct gpiod_lookup *table_entry,
58 struct acpi_resource_gpio *agpio,
59 const char *con_id, unsigned long gpio_flags)
60 {
61 char *path = agpio->resource_source.string_ptr;
62 struct acpi_device *adev;
63 acpi_handle handle;
64 acpi_status status;
65
66 status = acpi_get_handle(NULL, path, &handle);
67 if (ACPI_FAILURE(status))
68 return -EINVAL;
69
70 adev = acpi_fetch_acpi_dev(handle);
71 if (!adev)
72 return -ENODEV;
73
74 *table_entry = GPIO_LOOKUP(acpi_dev_name(adev), agpio->pin_table[0], con_id, gpio_flags);
75
76 return 0;
77 }
78
skl_int3472_map_gpio_to_sensor(struct int3472_discrete_device * int3472,struct acpi_resource_gpio * agpio,const char * con_id,unsigned long gpio_flags)79 static int skl_int3472_map_gpio_to_sensor(struct int3472_discrete_device *int3472,
80 struct acpi_resource_gpio *agpio,
81 const char *con_id, unsigned long gpio_flags)
82 {
83 int ret;
84
85 if (int3472->n_sensor_gpios >= INT3472_MAX_SENSOR_GPIOS) {
86 dev_warn(int3472->dev, "Too many GPIOs mapped\n");
87 return -EINVAL;
88 }
89
90 ret = skl_int3472_fill_gpiod_lookup(&int3472->gpios.table[int3472->n_sensor_gpios],
91 agpio, con_id, gpio_flags);
92 if (ret)
93 return ret;
94
95 int3472->n_sensor_gpios++;
96
97 return 0;
98 }
99
100 /* This should *really* only be used when there's no other way... */
101 static struct gpio_desc *
skl_int3472_gpiod_get_from_temp_lookup(struct int3472_discrete_device * int3472,struct acpi_resource_gpio * agpio,const char * con_id,unsigned long gpio_flags)102 skl_int3472_gpiod_get_from_temp_lookup(struct int3472_discrete_device *int3472,
103 struct acpi_resource_gpio *agpio,
104 const char *con_id, unsigned long gpio_flags)
105 {
106 struct gpio_desc *desc;
107 int ret;
108
109 struct gpiod_lookup_table *lookup __free(kfree) =
110 kzalloc(struct_size(lookup, table, 2), GFP_KERNEL);
111 if (!lookup)
112 return ERR_PTR(-ENOMEM);
113
114 lookup->dev_id = dev_name(int3472->dev);
115 ret = skl_int3472_fill_gpiod_lookup(&lookup->table[0], agpio, con_id, gpio_flags);
116 if (ret)
117 return ERR_PTR(ret);
118
119 gpiod_add_lookup_table(lookup);
120 desc = gpiod_get(int3472->dev, con_id, GPIOD_OUT_LOW);
121 gpiod_remove_lookup_table(lookup);
122
123 return desc;
124 }
125
126 /**
127 * struct int3472_gpio_map - Map GPIOs to whatever is expected by the
128 * sensor driver (as in DT bindings)
129 * @hid: The ACPI HID of the device without the instance number e.g. INT347E
130 * @type_from: The GPIO type from ACPI ?SDT
131 * @type_to: The assigned GPIO type, typically same as @type_from
132 * @con_id: The name of the GPIO for the device
133 * @polarity_low: GPIO_ACTIVE_LOW true if the @polarity_low is true,
134 * GPIO_ACTIVE_HIGH otherwise
135 */
136 struct int3472_gpio_map {
137 const char *hid;
138 u8 type_from;
139 u8 type_to;
140 bool polarity_low;
141 const char *con_id;
142 };
143
144 static const struct int3472_gpio_map int3472_gpio_map[] = {
145 /* mt9m114 designs declare a powerdown pin which controls the regulators */
146 { "INT33F0", INT3472_GPIO_TYPE_POWERDOWN, INT3472_GPIO_TYPE_POWER_ENABLE, false, "vdd" },
147 /* ov7251 driver / DT-bindings expect "enable" as con_id for reset */
148 { "INT347E", INT3472_GPIO_TYPE_RESET, INT3472_GPIO_TYPE_RESET, false, "enable" },
149 };
150
int3472_get_con_id_and_polarity(struct int3472_discrete_device * int3472,u8 * type,const char ** con_id,unsigned long * gpio_flags)151 static void int3472_get_con_id_and_polarity(struct int3472_discrete_device *int3472, u8 *type,
152 const char **con_id, unsigned long *gpio_flags)
153 {
154 struct acpi_device *adev = int3472->sensor;
155 unsigned int i;
156
157 for (i = 0; i < ARRAY_SIZE(int3472_gpio_map); i++) {
158 /*
159 * Map the firmware-provided GPIO to whatever a driver expects
160 * (as in DT bindings). First check if the type matches with the
161 * GPIO map, then further check that the device _HID matches.
162 */
163 if (*type != int3472_gpio_map[i].type_from)
164 continue;
165
166 if (!acpi_dev_hid_uid_match(adev, int3472_gpio_map[i].hid, NULL))
167 continue;
168
169 dev_dbg(int3472->dev, "mapping type 0x%02x pin to 0x%02x %s\n",
170 *type, int3472_gpio_map[i].type_to, int3472_gpio_map[i].con_id);
171
172 *type = int3472_gpio_map[i].type_to;
173 *gpio_flags = int3472_gpio_map[i].polarity_low ?
174 GPIO_ACTIVE_LOW : GPIO_ACTIVE_HIGH;
175 *con_id = int3472_gpio_map[i].con_id;
176 return;
177 }
178
179 switch (*type) {
180 case INT3472_GPIO_TYPE_RESET:
181 *con_id = "reset";
182 *gpio_flags = GPIO_ACTIVE_LOW;
183 break;
184 case INT3472_GPIO_TYPE_POWERDOWN:
185 *con_id = "powerdown";
186 *gpio_flags = GPIO_ACTIVE_LOW;
187 break;
188 case INT3472_GPIO_TYPE_CLK_ENABLE:
189 *con_id = "clk-enable";
190 *gpio_flags = GPIO_ACTIVE_HIGH;
191 break;
192 case INT3472_GPIO_TYPE_PRIVACY_LED:
193 *con_id = "privacy-led";
194 *gpio_flags = GPIO_ACTIVE_HIGH;
195 break;
196 case INT3472_GPIO_TYPE_POWER_ENABLE:
197 *con_id = "avdd";
198 *gpio_flags = GPIO_ACTIVE_HIGH;
199 break;
200 case INT3472_GPIO_TYPE_HANDSHAKE:
201 *con_id = "dvdd";
202 *gpio_flags = GPIO_ACTIVE_HIGH;
203 break;
204 default:
205 *con_id = "unknown";
206 *gpio_flags = GPIO_ACTIVE_HIGH;
207 break;
208 }
209 }
210
211 /**
212 * skl_int3472_handle_gpio_resources: Map PMIC resources to consuming sensor
213 * @ares: A pointer to a &struct acpi_resource
214 * @data: A pointer to a &struct int3472_discrete_device
215 *
216 * This function handles GPIO resources that are against an INT3472
217 * ACPI device, by checking the value of the corresponding _DSM entry.
218 * This will return a 32bit int, where the lowest byte represents the
219 * function of the GPIO pin:
220 *
221 * 0x00 Reset
222 * 0x01 Power down
223 * 0x0b Power enable
224 * 0x0c Clock enable
225 * 0x0d Privacy LED
226 *
227 * There are some known platform specific quirks where that does not quite
228 * hold up; for example where a pin with type 0x01 (Power down) is mapped to
229 * a sensor pin that performs a reset function or entries in _CRS and _DSM that
230 * do not actually correspond to a physical connection. These will be handled
231 * by the mapping sub-functions.
232 *
233 * GPIOs will either be mapped directly to the sensor device or else used
234 * to create clocks and regulators via the usual frameworks.
235 *
236 * Return:
237 * * 1 - Continue the loop without adding a copy of the resource to
238 * * the list passed to acpi_dev_get_resources()
239 * * 0 - Continue the loop after adding a copy of the resource to
240 * * the list passed to acpi_dev_get_resources()
241 * * -errno - Error, break loop
242 */
skl_int3472_handle_gpio_resources(struct acpi_resource * ares,void * data)243 static int skl_int3472_handle_gpio_resources(struct acpi_resource *ares,
244 void *data)
245 {
246 struct int3472_discrete_device *int3472 = data;
247 struct acpi_resource_gpio *agpio;
248 u8 active_value, pin, type;
249 union acpi_object *obj;
250 struct gpio_desc *gpio;
251 const char *err_msg;
252 const char *con_id;
253 unsigned long gpio_flags;
254 int ret;
255
256 if (!acpi_gpio_get_io_resource(ares, &agpio))
257 return 1;
258
259 /*
260 * ngpios + 2 because the index of this _DSM function is 1-based and
261 * the first function is just a count.
262 */
263 obj = acpi_evaluate_dsm_typed(int3472->adev->handle,
264 &int3472_gpio_guid, 0x00,
265 int3472->ngpios + 2,
266 NULL, ACPI_TYPE_INTEGER);
267
268 if (!obj) {
269 dev_warn(int3472->dev, "No _DSM entry for GPIO pin %u\n",
270 agpio->pin_table[0]);
271 return 1;
272 }
273
274 type = FIELD_GET(INT3472_GPIO_DSM_TYPE, obj->integer.value);
275
276 int3472_get_con_id_and_polarity(int3472, &type, &con_id, &gpio_flags);
277
278 pin = FIELD_GET(INT3472_GPIO_DSM_PIN, obj->integer.value);
279 /* Pin field is not really used under Windows and wraps around at 8 bits */
280 if (pin != (agpio->pin_table[0] & 0xff))
281 dev_dbg(int3472->dev, FW_BUG "%s %s pin number mismatch _DSM %d resource %d\n",
282 con_id, agpio->resource_source.string_ptr, pin, agpio->pin_table[0]);
283
284 active_value = FIELD_GET(INT3472_GPIO_DSM_SENSOR_ON_VAL, obj->integer.value);
285 if (!active_value)
286 gpio_flags ^= GPIO_ACTIVE_LOW;
287
288 dev_dbg(int3472->dev, "%s %s pin %d active-%s\n", con_id,
289 agpio->resource_source.string_ptr, agpio->pin_table[0],
290 str_high_low(gpio_flags == GPIO_ACTIVE_HIGH));
291
292 switch (type) {
293 case INT3472_GPIO_TYPE_RESET:
294 case INT3472_GPIO_TYPE_POWERDOWN:
295 ret = skl_int3472_map_gpio_to_sensor(int3472, agpio, con_id, gpio_flags);
296 if (ret)
297 err_msg = "Failed to map GPIO pin to sensor\n";
298
299 break;
300 case INT3472_GPIO_TYPE_CLK_ENABLE:
301 case INT3472_GPIO_TYPE_PRIVACY_LED:
302 case INT3472_GPIO_TYPE_POWER_ENABLE:
303 case INT3472_GPIO_TYPE_HANDSHAKE:
304 gpio = skl_int3472_gpiod_get_from_temp_lookup(int3472, agpio, con_id, gpio_flags);
305 if (IS_ERR(gpio)) {
306 ret = PTR_ERR(gpio);
307 err_msg = "Failed to get GPIO\n";
308 break;
309 }
310
311 switch (type) {
312 case INT3472_GPIO_TYPE_CLK_ENABLE:
313 ret = skl_int3472_register_gpio_clock(int3472, gpio);
314 if (ret)
315 err_msg = "Failed to register clock\n";
316
317 break;
318 case INT3472_GPIO_TYPE_PRIVACY_LED:
319 ret = skl_int3472_register_pled(int3472, gpio);
320 if (ret)
321 err_msg = "Failed to register LED\n";
322
323 break;
324 case INT3472_GPIO_TYPE_POWER_ENABLE:
325 ret = skl_int3472_register_regulator(int3472, gpio,
326 GPIO_REGULATOR_ENABLE_TIME,
327 con_id,
328 int3472->quirks.avdd_second_sensor);
329 if (ret)
330 err_msg = "Failed to map power-enable to sensor\n";
331
332 break;
333 case INT3472_GPIO_TYPE_HANDSHAKE:
334 /* Setups using a handshake pin need 25 ms enable delay */
335 ret = skl_int3472_register_regulator(int3472, gpio,
336 25 * USEC_PER_MSEC,
337 con_id, NULL);
338 if (ret)
339 err_msg = "Failed to map handshake to sensor\n";
340
341 break;
342 default: /* Never reached */
343 ret = -EINVAL;
344 break;
345 }
346
347 if (ret)
348 gpiod_put(gpio);
349
350 break;
351 default:
352 dev_warn(int3472->dev,
353 "GPIO type 0x%02x unknown; the sensor may not work\n",
354 type);
355 ret = 1;
356 break;
357 }
358
359 int3472->ngpios++;
360 ACPI_FREE(obj);
361
362 if (ret < 0)
363 return dev_err_probe(int3472->dev, ret, err_msg);
364
365 /* Tell acpi_dev_get_resources() to not make a copy of the resource */
366 return 1;
367 }
368
int3472_discrete_parse_crs(struct int3472_discrete_device * int3472)369 int int3472_discrete_parse_crs(struct int3472_discrete_device *int3472)
370 {
371 LIST_HEAD(resource_list);
372 int ret;
373
374 skl_int3472_log_sensor_module_name(int3472);
375
376 ret = acpi_dev_get_resources(int3472->adev, &resource_list,
377 skl_int3472_handle_gpio_resources,
378 int3472);
379 if (ret < 0)
380 return ret;
381
382 acpi_dev_free_resource_list(&resource_list);
383
384 /* Register _DSM based clock (no-op if a GPIO clock was already registered) */
385 ret = skl_int3472_register_dsm_clock(int3472);
386 if (ret < 0)
387 return ret;
388
389 int3472->gpios.dev_id = int3472->sensor_name;
390 gpiod_add_lookup_table(&int3472->gpios);
391
392 return 0;
393 }
394 EXPORT_SYMBOL_NS_GPL(int3472_discrete_parse_crs, "INTEL_INT3472_DISCRETE");
395
int3472_discrete_cleanup(struct int3472_discrete_device * int3472)396 void int3472_discrete_cleanup(struct int3472_discrete_device *int3472)
397 {
398 gpiod_remove_lookup_table(&int3472->gpios);
399
400 skl_int3472_unregister_clock(int3472);
401 skl_int3472_unregister_pled(int3472);
402 skl_int3472_unregister_regulator(int3472);
403 }
404 EXPORT_SYMBOL_NS_GPL(int3472_discrete_cleanup, "INTEL_INT3472_DISCRETE");
405
skl_int3472_discrete_remove(struct platform_device * pdev)406 static void skl_int3472_discrete_remove(struct platform_device *pdev)
407 {
408 int3472_discrete_cleanup(platform_get_drvdata(pdev));
409 }
410
skl_int3472_discrete_probe(struct platform_device * pdev)411 static int skl_int3472_discrete_probe(struct platform_device *pdev)
412 {
413 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
414 const struct int3472_discrete_quirks *quirks = NULL;
415 struct int3472_discrete_device *int3472;
416 const struct dmi_system_id *id;
417 struct int3472_cldb cldb;
418 int ret;
419
420 if (!adev)
421 return -ENODEV;
422
423 id = dmi_first_match(skl_int3472_discrete_quirks);
424 if (id)
425 quirks = id->driver_data;
426
427 ret = skl_int3472_fill_cldb(adev, &cldb);
428 if (ret) {
429 dev_err(&pdev->dev, "Couldn't fill CLDB structure\n");
430 return ret;
431 }
432
433 if (cldb.control_logic_type != 1) {
434 dev_err(&pdev->dev, "Unsupported control logic type %u\n",
435 cldb.control_logic_type);
436 return -EINVAL;
437 }
438
439 /* Max num GPIOs we've seen plus a terminator */
440 int3472 = devm_kzalloc(&pdev->dev, struct_size(int3472, gpios.table,
441 INT3472_MAX_SENSOR_GPIOS + 1), GFP_KERNEL);
442 if (!int3472)
443 return -ENOMEM;
444
445 int3472->adev = adev;
446 int3472->dev = &pdev->dev;
447 platform_set_drvdata(pdev, int3472);
448 int3472->clock.imgclk_index = cldb.clock_source;
449
450 if (quirks)
451 int3472->quirks = *quirks;
452
453 ret = skl_int3472_get_sensor_adev_and_name(&pdev->dev, &int3472->sensor,
454 &int3472->sensor_name);
455 if (ret)
456 return ret;
457
458 /*
459 * Initialising this list means we can call gpiod_remove_lookup_table()
460 * in failure paths without issue.
461 */
462 INIT_LIST_HEAD(&int3472->gpios.list);
463
464 ret = int3472_discrete_parse_crs(int3472);
465 if (ret) {
466 skl_int3472_discrete_remove(pdev);
467 return ret;
468 }
469
470 acpi_dev_clear_dependencies(adev);
471 return 0;
472 }
473
474 static const struct acpi_device_id int3472_device_id[] = {
475 { "INT3472", 0 },
476 { }
477 };
478 MODULE_DEVICE_TABLE(acpi, int3472_device_id);
479
480 static struct platform_driver int3472_discrete = {
481 .driver = {
482 .name = "int3472-discrete",
483 .acpi_match_table = int3472_device_id,
484 },
485 .probe = skl_int3472_discrete_probe,
486 .remove = skl_int3472_discrete_remove,
487 };
488 module_platform_driver(int3472_discrete);
489
490 MODULE_DESCRIPTION("Intel SkyLake INT3472 ACPI Discrete Device Driver");
491 MODULE_AUTHOR("Daniel Scally <djrscally@gmail.com>");
492 MODULE_LICENSE("GPL v2");
493 MODULE_IMPORT_NS("INTEL_INT3472");
494