1 // SPDX-License-Identifier: GPL-2.0
2 /* Author: Dan Scally <djrscally@gmail.com> */
3
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>
9 #include <linux/i2c.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/uuid.h>
15
16 #include "common.h"
17
18 /*
19 * 79234640-9e10-4fea-a5c1-b5aa8b19756f
20 * This _DSM GUID returns information about the GPIO lines mapped to a
21 * discrete INT3472 device. Function number 1 returns a count of the GPIO
22 * lines that are mapped. Subsequent functions return 32 bit ints encoding
23 * information about the GPIO line, including its purpose.
24 */
25 static const guid_t int3472_gpio_guid =
26 GUID_INIT(0x79234640, 0x9e10, 0x4fea,
27 0xa5, 0xc1, 0xb5, 0xaa, 0x8b, 0x19, 0x75, 0x6f);
28
29 #define INT3472_GPIO_DSM_TYPE GENMASK(7, 0)
30 #define INT3472_GPIO_DSM_PIN GENMASK(15, 8)
31 #define INT3472_GPIO_DSM_SENSOR_ON_VAL GENMASK(31, 24)
32
33 /*
34 * 822ace8f-2814-4174-a56b-5f029fe079ee
35 * This _DSM GUID returns a string from the sensor device, which acts as a
36 * module identifier.
37 */
38 static const guid_t cio2_sensor_module_guid =
39 GUID_INIT(0x822ace8f, 0x2814, 0x4174,
40 0xa5, 0x6b, 0x5f, 0x02, 0x9f, 0xe0, 0x79, 0xee);
41
skl_int3472_log_sensor_module_name(struct int3472_discrete_device * int3472)42 static void skl_int3472_log_sensor_module_name(struct int3472_discrete_device *int3472)
43 {
44 union acpi_object *obj;
45
46 obj = acpi_evaluate_dsm_typed(int3472->sensor->handle,
47 &cio2_sensor_module_guid, 0x00,
48 0x01, NULL, ACPI_TYPE_STRING);
49 if (obj) {
50 dev_dbg(int3472->dev, "Sensor module id: '%s'\n", obj->string.pointer);
51 ACPI_FREE(obj);
52 }
53 }
54
skl_int3472_map_gpio_to_sensor(struct int3472_discrete_device * int3472,struct acpi_resource_gpio * agpio,const char * func,u32 polarity)55 static int skl_int3472_map_gpio_to_sensor(struct int3472_discrete_device *int3472,
56 struct acpi_resource_gpio *agpio,
57 const char *func, u32 polarity)
58 {
59 char *path = agpio->resource_source.string_ptr;
60 struct gpiod_lookup *table_entry;
61 struct acpi_device *adev;
62 acpi_handle handle;
63 acpi_status status;
64
65 if (int3472->n_sensor_gpios >= INT3472_MAX_SENSOR_GPIOS) {
66 dev_warn(int3472->dev, "Too many GPIOs mapped\n");
67 return -EINVAL;
68 }
69
70 status = acpi_get_handle(NULL, path, &handle);
71 if (ACPI_FAILURE(status))
72 return -EINVAL;
73
74 adev = acpi_fetch_acpi_dev(handle);
75 if (!adev)
76 return -ENODEV;
77
78 table_entry = &int3472->gpios.table[int3472->n_sensor_gpios];
79 table_entry->key = acpi_dev_name(adev);
80 table_entry->chip_hwnum = agpio->pin_table[0];
81 table_entry->con_id = func;
82 table_entry->idx = 0;
83 table_entry->flags = polarity;
84
85 int3472->n_sensor_gpios++;
86
87 return 0;
88 }
89
int3472_get_func_and_polarity(u8 type,const char ** func,u32 * polarity)90 static void int3472_get_func_and_polarity(u8 type, const char **func, u32 *polarity)
91 {
92 switch (type) {
93 case INT3472_GPIO_TYPE_RESET:
94 *func = "reset";
95 *polarity = GPIO_ACTIVE_LOW;
96 break;
97 case INT3472_GPIO_TYPE_POWERDOWN:
98 *func = "powerdown";
99 *polarity = GPIO_ACTIVE_LOW;
100 break;
101 case INT3472_GPIO_TYPE_CLK_ENABLE:
102 *func = "clk-enable";
103 *polarity = GPIO_ACTIVE_HIGH;
104 break;
105 case INT3472_GPIO_TYPE_PRIVACY_LED:
106 *func = "privacy-led";
107 *polarity = GPIO_ACTIVE_HIGH;
108 break;
109 case INT3472_GPIO_TYPE_POWER_ENABLE:
110 *func = "power-enable";
111 *polarity = GPIO_ACTIVE_HIGH;
112 break;
113 default:
114 *func = "unknown";
115 *polarity = GPIO_ACTIVE_HIGH;
116 break;
117 }
118 }
119
120 /**
121 * skl_int3472_handle_gpio_resources: Map PMIC resources to consuming sensor
122 * @ares: A pointer to a &struct acpi_resource
123 * @data: A pointer to a &struct int3472_discrete_device
124 *
125 * This function handles GPIO resources that are against an INT3472
126 * ACPI device, by checking the value of the corresponding _DSM entry.
127 * This will return a 32bit int, where the lowest byte represents the
128 * function of the GPIO pin:
129 *
130 * 0x00 Reset
131 * 0x01 Power down
132 * 0x0b Power enable
133 * 0x0c Clock enable
134 * 0x0d Privacy LED
135 *
136 * There are some known platform specific quirks where that does not quite
137 * hold up; for example where a pin with type 0x01 (Power down) is mapped to
138 * a sensor pin that performs a reset function or entries in _CRS and _DSM that
139 * do not actually correspond to a physical connection. These will be handled
140 * by the mapping sub-functions.
141 *
142 * GPIOs will either be mapped directly to the sensor device or else used
143 * to create clocks and regulators via the usual frameworks.
144 *
145 * Return:
146 * * 1 - To continue the loop
147 * * 0 - When all resources found are handled properly.
148 * * -EINVAL - If the resource is not a GPIO IO resource
149 * * -ENODEV - If the resource has no corresponding _DSM entry
150 * * -Other - Errors propagated from one of the sub-functions.
151 */
skl_int3472_handle_gpio_resources(struct acpi_resource * ares,void * data)152 static int skl_int3472_handle_gpio_resources(struct acpi_resource *ares,
153 void *data)
154 {
155 struct int3472_discrete_device *int3472 = data;
156 struct acpi_resource_gpio *agpio;
157 u8 active_value, pin, type;
158 union acpi_object *obj;
159 const char *err_msg;
160 const char *func;
161 u32 polarity;
162 int ret;
163
164 if (!acpi_gpio_get_io_resource(ares, &agpio))
165 return 1;
166
167 /*
168 * ngpios + 2 because the index of this _DSM function is 1-based and
169 * the first function is just a count.
170 */
171 obj = acpi_evaluate_dsm_typed(int3472->adev->handle,
172 &int3472_gpio_guid, 0x00,
173 int3472->ngpios + 2,
174 NULL, ACPI_TYPE_INTEGER);
175
176 if (!obj) {
177 dev_warn(int3472->dev, "No _DSM entry for GPIO pin %u\n",
178 agpio->pin_table[0]);
179 return 1;
180 }
181
182 type = FIELD_GET(INT3472_GPIO_DSM_TYPE, obj->integer.value);
183
184 int3472_get_func_and_polarity(type, &func, &polarity);
185
186 pin = FIELD_GET(INT3472_GPIO_DSM_PIN, obj->integer.value);
187 if (pin != agpio->pin_table[0])
188 dev_warn(int3472->dev, "%s %s pin number mismatch _DSM %d resource %d\n",
189 func, agpio->resource_source.string_ptr, pin,
190 agpio->pin_table[0]);
191
192 active_value = FIELD_GET(INT3472_GPIO_DSM_SENSOR_ON_VAL, obj->integer.value);
193 if (!active_value)
194 polarity ^= GPIO_ACTIVE_LOW;
195
196 dev_dbg(int3472->dev, "%s %s pin %d active-%s\n", func,
197 agpio->resource_source.string_ptr, agpio->pin_table[0],
198 (polarity == GPIO_ACTIVE_HIGH) ? "high" : "low");
199
200 switch (type) {
201 case INT3472_GPIO_TYPE_RESET:
202 case INT3472_GPIO_TYPE_POWERDOWN:
203 ret = skl_int3472_map_gpio_to_sensor(int3472, agpio, func, polarity);
204 if (ret)
205 err_msg = "Failed to map GPIO pin to sensor\n";
206
207 break;
208 case INT3472_GPIO_TYPE_CLK_ENABLE:
209 ret = skl_int3472_register_gpio_clock(int3472, agpio, polarity);
210 if (ret)
211 err_msg = "Failed to register clock\n";
212
213 break;
214 case INT3472_GPIO_TYPE_PRIVACY_LED:
215 ret = skl_int3472_register_pled(int3472, agpio, polarity);
216 if (ret)
217 err_msg = "Failed to register LED\n";
218
219 break;
220 case INT3472_GPIO_TYPE_POWER_ENABLE:
221 ret = skl_int3472_register_regulator(int3472, agpio);
222 if (ret)
223 err_msg = "Failed to map regulator to sensor\n";
224
225 break;
226 default:
227 dev_warn(int3472->dev,
228 "GPIO type 0x%02x unknown; the sensor may not work\n",
229 type);
230 ret = 1;
231 break;
232 }
233
234 int3472->ngpios++;
235 ACPI_FREE(obj);
236
237 if (ret < 0)
238 return dev_err_probe(int3472->dev, ret, err_msg);
239
240 return ret;
241 }
242
skl_int3472_parse_crs(struct int3472_discrete_device * int3472)243 static int skl_int3472_parse_crs(struct int3472_discrete_device *int3472)
244 {
245 LIST_HEAD(resource_list);
246 int ret;
247
248 skl_int3472_log_sensor_module_name(int3472);
249
250 ret = acpi_dev_get_resources(int3472->adev, &resource_list,
251 skl_int3472_handle_gpio_resources,
252 int3472);
253 if (ret < 0)
254 return ret;
255
256 acpi_dev_free_resource_list(&resource_list);
257
258 /* Register _DSM based clock (no-op if a GPIO clock was already registered) */
259 ret = skl_int3472_register_dsm_clock(int3472);
260 if (ret < 0)
261 return ret;
262
263 int3472->gpios.dev_id = int3472->sensor_name;
264 gpiod_add_lookup_table(&int3472->gpios);
265
266 return 0;
267 }
268
skl_int3472_discrete_remove(struct platform_device * pdev)269 static void skl_int3472_discrete_remove(struct platform_device *pdev)
270 {
271 struct int3472_discrete_device *int3472 = platform_get_drvdata(pdev);
272
273 gpiod_remove_lookup_table(&int3472->gpios);
274
275 skl_int3472_unregister_clock(int3472);
276 skl_int3472_unregister_pled(int3472);
277 skl_int3472_unregister_regulator(int3472);
278 }
279
skl_int3472_discrete_probe(struct platform_device * pdev)280 static int skl_int3472_discrete_probe(struct platform_device *pdev)
281 {
282 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
283 struct int3472_discrete_device *int3472;
284 struct int3472_cldb cldb;
285 int ret;
286
287 ret = skl_int3472_fill_cldb(adev, &cldb);
288 if (ret) {
289 dev_err(&pdev->dev, "Couldn't fill CLDB structure\n");
290 return ret;
291 }
292
293 if (cldb.control_logic_type != 1) {
294 dev_err(&pdev->dev, "Unsupported control logic type %u\n",
295 cldb.control_logic_type);
296 return -EINVAL;
297 }
298
299 /* Max num GPIOs we've seen plus a terminator */
300 int3472 = devm_kzalloc(&pdev->dev, struct_size(int3472, gpios.table,
301 INT3472_MAX_SENSOR_GPIOS + 1), GFP_KERNEL);
302 if (!int3472)
303 return -ENOMEM;
304
305 int3472->adev = adev;
306 int3472->dev = &pdev->dev;
307 platform_set_drvdata(pdev, int3472);
308 int3472->clock.imgclk_index = cldb.clock_source;
309
310 ret = skl_int3472_get_sensor_adev_and_name(&pdev->dev, &int3472->sensor,
311 &int3472->sensor_name);
312 if (ret)
313 return ret;
314
315 /*
316 * Initialising this list means we can call gpiod_remove_lookup_table()
317 * in failure paths without issue.
318 */
319 INIT_LIST_HEAD(&int3472->gpios.list);
320
321 ret = skl_int3472_parse_crs(int3472);
322 if (ret) {
323 skl_int3472_discrete_remove(pdev);
324 return ret;
325 }
326
327 acpi_dev_clear_dependencies(adev);
328 return 0;
329 }
330
331 static const struct acpi_device_id int3472_device_id[] = {
332 { "INT3472", 0 },
333 { }
334 };
335 MODULE_DEVICE_TABLE(acpi, int3472_device_id);
336
337 static struct platform_driver int3472_discrete = {
338 .driver = {
339 .name = "int3472-discrete",
340 .acpi_match_table = int3472_device_id,
341 },
342 .probe = skl_int3472_discrete_probe,
343 .remove_new = skl_int3472_discrete_remove,
344 };
345 module_platform_driver(int3472_discrete);
346
347 MODULE_DESCRIPTION("Intel SkyLake INT3472 ACPI Discrete Device Driver");
348 MODULE_AUTHOR("Daniel Scally <djrscally@gmail.com>");
349 MODULE_LICENSE("GPL v2");
350