Merge tag 'driver-core-5.3-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / drivers / i2c / i2c-core-acpi.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Linux I2C core ACPI support code
4  *
5  * Copyright (C) 2014 Intel Corp, Author: Lan Tianyu <tianyu.lan@intel.com>
6  */
7
8 #include <linux/acpi.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/i2c.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15
16 #include "i2c-core.h"
17
18 struct i2c_acpi_handler_data {
19         struct acpi_connection_info info;
20         struct i2c_adapter *adapter;
21 };
22
23 struct gsb_buffer {
24         u8      status;
25         u8      len;
26         union {
27                 u16     wdata;
28                 u8      bdata;
29                 u8      data[0];
30         };
31 } __packed;
32
33 struct i2c_acpi_lookup {
34         struct i2c_board_info *info;
35         acpi_handle adapter_handle;
36         acpi_handle device_handle;
37         acpi_handle search_handle;
38         int n;
39         int index;
40         u32 speed;
41         u32 min_speed;
42 };
43
44 /**
45  * i2c_acpi_get_i2c_resource - Gets I2cSerialBus resource if type matches
46  * @ares:       ACPI resource
47  * @i2c:        Pointer to I2cSerialBus resource will be returned here
48  *
49  * Checks if the given ACPI resource is of type I2cSerialBus.
50  * In this case, returns a pointer to it to the caller.
51  *
52  * Returns true if resource type is of I2cSerialBus, otherwise false.
53  */
54 bool i2c_acpi_get_i2c_resource(struct acpi_resource *ares,
55                                struct acpi_resource_i2c_serialbus **i2c)
56 {
57         struct acpi_resource_i2c_serialbus *sb;
58
59         if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
60                 return false;
61
62         sb = &ares->data.i2c_serial_bus;
63         if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
64                 return false;
65
66         *i2c = sb;
67         return true;
68 }
69 EXPORT_SYMBOL_GPL(i2c_acpi_get_i2c_resource);
70
71 static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data)
72 {
73         struct i2c_acpi_lookup *lookup = data;
74         struct i2c_board_info *info = lookup->info;
75         struct acpi_resource_i2c_serialbus *sb;
76         acpi_status status;
77
78         if (info->addr || !i2c_acpi_get_i2c_resource(ares, &sb))
79                 return 1;
80
81         if (lookup->index != -1 && lookup->n++ != lookup->index)
82                 return 1;
83
84         status = acpi_get_handle(lookup->device_handle,
85                                  sb->resource_source.string_ptr,
86                                  &lookup->adapter_handle);
87         if (ACPI_FAILURE(status))
88                 return 1;
89
90         info->addr = sb->slave_address;
91         lookup->speed = sb->connection_speed;
92         if (sb->access_mode == ACPI_I2C_10BIT_MODE)
93                 info->flags |= I2C_CLIENT_TEN;
94
95         return 1;
96 }
97
98 static const struct acpi_device_id i2c_acpi_ignored_device_ids[] = {
99         /*
100          * ACPI video acpi_devices, which are handled by the acpi-video driver
101          * sometimes contain a SERIAL_TYPE_I2C ACPI resource, ignore these.
102          */
103         { ACPI_VIDEO_HID, 0 },
104         {}
105 };
106
107 static int i2c_acpi_do_lookup(struct acpi_device *adev,
108                               struct i2c_acpi_lookup *lookup)
109 {
110         struct i2c_board_info *info = lookup->info;
111         struct list_head resource_list;
112         int ret;
113
114         if (acpi_bus_get_status(adev) || !adev->status.present)
115                 return -EINVAL;
116
117         if (acpi_match_device_ids(adev, i2c_acpi_ignored_device_ids) == 0)
118                 return -ENODEV;
119
120         memset(info, 0, sizeof(*info));
121         lookup->device_handle = acpi_device_handle(adev);
122
123         /* Look up for I2cSerialBus resource */
124         INIT_LIST_HEAD(&resource_list);
125         ret = acpi_dev_get_resources(adev, &resource_list,
126                                      i2c_acpi_fill_info, lookup);
127         acpi_dev_free_resource_list(&resource_list);
128
129         if (ret < 0 || !info->addr)
130                 return -EINVAL;
131
132         return 0;
133 }
134
135 static int i2c_acpi_get_info(struct acpi_device *adev,
136                              struct i2c_board_info *info,
137                              struct i2c_adapter *adapter,
138                              acpi_handle *adapter_handle)
139 {
140         struct list_head resource_list;
141         struct resource_entry *entry;
142         struct i2c_acpi_lookup lookup;
143         int ret;
144
145         memset(&lookup, 0, sizeof(lookup));
146         lookup.info = info;
147         lookup.index = -1;
148
149         if (acpi_device_enumerated(adev))
150                 return -EINVAL;
151
152         ret = i2c_acpi_do_lookup(adev, &lookup);
153         if (ret)
154                 return ret;
155
156         if (adapter) {
157                 /* The adapter must match the one in I2cSerialBus() connector */
158                 if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle)
159                         return -ENODEV;
160         } else {
161                 struct acpi_device *adapter_adev;
162
163                 /* The adapter must be present */
164                 if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev))
165                         return -ENODEV;
166                 if (acpi_bus_get_status(adapter_adev) ||
167                     !adapter_adev->status.present)
168                         return -ENODEV;
169         }
170
171         info->fwnode = acpi_fwnode_handle(adev);
172         if (adapter_handle)
173                 *adapter_handle = lookup.adapter_handle;
174
175         /* Then fill IRQ number if any */
176         INIT_LIST_HEAD(&resource_list);
177         ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
178         if (ret < 0)
179                 return -EINVAL;
180
181         resource_list_for_each_entry(entry, &resource_list) {
182                 if (resource_type(entry->res) == IORESOURCE_IRQ) {
183                         info->irq = entry->res->start;
184                         break;
185                 }
186         }
187
188         acpi_dev_free_resource_list(&resource_list);
189
190         acpi_set_modalias(adev, dev_name(&adev->dev), info->type,
191                           sizeof(info->type));
192
193         return 0;
194 }
195
196 static void i2c_acpi_register_device(struct i2c_adapter *adapter,
197                                      struct acpi_device *adev,
198                                      struct i2c_board_info *info)
199 {
200         adev->power.flags.ignore_parent = true;
201         acpi_device_set_enumerated(adev);
202
203         if (!i2c_new_device(adapter, info)) {
204                 adev->power.flags.ignore_parent = false;
205                 dev_err(&adapter->dev,
206                         "failed to add I2C device %s from ACPI\n",
207                         dev_name(&adev->dev));
208         }
209 }
210
211 static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
212                                        void *data, void **return_value)
213 {
214         struct i2c_adapter *adapter = data;
215         struct acpi_device *adev;
216         struct i2c_board_info info;
217
218         if (acpi_bus_get_device(handle, &adev))
219                 return AE_OK;
220
221         if (i2c_acpi_get_info(adev, &info, adapter, NULL))
222                 return AE_OK;
223
224         i2c_acpi_register_device(adapter, adev, &info);
225
226         return AE_OK;
227 }
228
229 #define I2C_ACPI_MAX_SCAN_DEPTH 32
230
231 /**
232  * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
233  * @adap: pointer to adapter
234  *
235  * Enumerate all I2C slave devices behind this adapter by walking the ACPI
236  * namespace. When a device is found it will be added to the Linux device
237  * model and bound to the corresponding ACPI handle.
238  */
239 void i2c_acpi_register_devices(struct i2c_adapter *adap)
240 {
241         acpi_status status;
242
243         if (!has_acpi_companion(&adap->dev))
244                 return;
245
246         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
247                                      I2C_ACPI_MAX_SCAN_DEPTH,
248                                      i2c_acpi_add_device, NULL,
249                                      adap, NULL);
250         if (ACPI_FAILURE(status))
251                 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
252 }
253
254 const struct acpi_device_id *
255 i2c_acpi_match_device(const struct acpi_device_id *matches,
256                       struct i2c_client *client)
257 {
258         if (!(client && matches))
259                 return NULL;
260
261         return acpi_match_device(matches, &client->dev);
262 }
263
264 static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
265                                            void *data, void **return_value)
266 {
267         struct i2c_acpi_lookup *lookup = data;
268         struct acpi_device *adev;
269
270         if (acpi_bus_get_device(handle, &adev))
271                 return AE_OK;
272
273         if (i2c_acpi_do_lookup(adev, lookup))
274                 return AE_OK;
275
276         if (lookup->search_handle != lookup->adapter_handle)
277                 return AE_OK;
278
279         if (lookup->speed <= lookup->min_speed)
280                 lookup->min_speed = lookup->speed;
281
282         return AE_OK;
283 }
284
285 /**
286  * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
287  * @dev: The device owning the bus
288  *
289  * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
290  * devices connected to this bus and use the speed of slowest device.
291  *
292  * Returns the speed in Hz or zero
293  */
294 u32 i2c_acpi_find_bus_speed(struct device *dev)
295 {
296         struct i2c_acpi_lookup lookup;
297         struct i2c_board_info dummy;
298         acpi_status status;
299
300         if (!has_acpi_companion(dev))
301                 return 0;
302
303         memset(&lookup, 0, sizeof(lookup));
304         lookup.search_handle = ACPI_HANDLE(dev);
305         lookup.min_speed = UINT_MAX;
306         lookup.info = &dummy;
307         lookup.index = -1;
308
309         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
310                                      I2C_ACPI_MAX_SCAN_DEPTH,
311                                      i2c_acpi_lookup_speed, NULL,
312                                      &lookup, NULL);
313
314         if (ACPI_FAILURE(status)) {
315                 dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
316                 return 0;
317         }
318
319         return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0;
320 }
321 EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
322
323 static int i2c_acpi_find_match_adapter(struct device *dev, const void *data)
324 {
325         struct i2c_adapter *adapter = i2c_verify_adapter(dev);
326
327         if (!adapter)
328                 return 0;
329
330         return ACPI_HANDLE(dev) == (acpi_handle)data;
331 }
332
333 static int i2c_acpi_find_match_device(struct device *dev, const void *data)
334 {
335         return ACPI_COMPANION(dev) == data;
336 }
337
338 struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
339 {
340         struct device *dev;
341
342         dev = bus_find_device(&i2c_bus_type, NULL, handle,
343                               i2c_acpi_find_match_adapter);
344         return dev ? i2c_verify_adapter(dev) : NULL;
345 }
346 EXPORT_SYMBOL_GPL(i2c_acpi_find_adapter_by_handle);
347
348 static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
349 {
350         struct device *dev;
351
352         dev = bus_find_device(&i2c_bus_type, NULL, adev,
353                               i2c_acpi_find_match_device);
354         return dev ? i2c_verify_client(dev) : NULL;
355 }
356
357 static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
358                            void *arg)
359 {
360         struct acpi_device *adev = arg;
361         struct i2c_board_info info;
362         acpi_handle adapter_handle;
363         struct i2c_adapter *adapter;
364         struct i2c_client *client;
365
366         switch (value) {
367         case ACPI_RECONFIG_DEVICE_ADD:
368                 if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
369                         break;
370
371                 adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
372                 if (!adapter)
373                         break;
374
375                 i2c_acpi_register_device(adapter, adev, &info);
376                 break;
377         case ACPI_RECONFIG_DEVICE_REMOVE:
378                 if (!acpi_device_enumerated(adev))
379                         break;
380
381                 client = i2c_acpi_find_client_by_adev(adev);
382                 if (!client)
383                         break;
384
385                 i2c_unregister_device(client);
386                 put_device(&client->dev);
387                 break;
388         }
389
390         return NOTIFY_OK;
391 }
392
393 struct notifier_block i2c_acpi_notifier = {
394         .notifier_call = i2c_acpi_notify,
395 };
396
397 /**
398  * i2c_acpi_new_device - Create i2c-client for the Nth I2cSerialBus resource
399  * @dev:     Device owning the ACPI resources to get the client from
400  * @index:   Index of ACPI resource to get
401  * @info:    describes the I2C device; note this is modified (addr gets set)
402  * Context: can sleep
403  *
404  * By default the i2c subsys creates an i2c-client for the first I2cSerialBus
405  * resource of an acpi_device, but some acpi_devices have multiple I2cSerialBus
406  * resources, in that case this function can be used to create an i2c-client
407  * for other I2cSerialBus resources in the Current Resource Settings table.
408  *
409  * Also see i2c_new_device, which this function calls to create the i2c-client.
410  *
411  * Returns a pointer to the new i2c-client, or error pointer in case of failure.
412  * Specifically, -EPROBE_DEFER is returned if the adapter is not found.
413  */
414 struct i2c_client *i2c_acpi_new_device(struct device *dev, int index,
415                                        struct i2c_board_info *info)
416 {
417         struct i2c_acpi_lookup lookup;
418         struct i2c_adapter *adapter;
419         struct i2c_client *client;
420         struct acpi_device *adev;
421         LIST_HEAD(resource_list);
422         int ret;
423
424         adev = ACPI_COMPANION(dev);
425         if (!adev)
426                 return ERR_PTR(-EINVAL);
427
428         memset(&lookup, 0, sizeof(lookup));
429         lookup.info = info;
430         lookup.device_handle = acpi_device_handle(adev);
431         lookup.index = index;
432
433         ret = acpi_dev_get_resources(adev, &resource_list,
434                                      i2c_acpi_fill_info, &lookup);
435         if (ret < 0)
436                 return ERR_PTR(ret);
437
438         acpi_dev_free_resource_list(&resource_list);
439
440         if (!info->addr)
441                 return ERR_PTR(-EADDRNOTAVAIL);
442
443         adapter = i2c_acpi_find_adapter_by_handle(lookup.adapter_handle);
444         if (!adapter)
445                 return ERR_PTR(-EPROBE_DEFER);
446
447         client = i2c_new_device(adapter, info);
448         if (!client)
449                 return ERR_PTR(-ENODEV);
450
451         return client;
452 }
453 EXPORT_SYMBOL_GPL(i2c_acpi_new_device);
454
455 #ifdef CONFIG_ACPI_I2C_OPREGION
456 static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
457                 u8 cmd, u8 *data, u8 data_len)
458 {
459
460         struct i2c_msg msgs[2];
461         int ret;
462         u8 *buffer;
463
464         buffer = kzalloc(data_len, GFP_KERNEL);
465         if (!buffer)
466                 return AE_NO_MEMORY;
467
468         msgs[0].addr = client->addr;
469         msgs[0].flags = client->flags;
470         msgs[0].len = 1;
471         msgs[0].buf = &cmd;
472
473         msgs[1].addr = client->addr;
474         msgs[1].flags = client->flags | I2C_M_RD;
475         msgs[1].len = data_len;
476         msgs[1].buf = buffer;
477
478         ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
479         if (ret < 0) {
480                 /* Getting a NACK is unfortunately normal with some DSTDs */
481                 if (ret == -EREMOTEIO)
482                         dev_dbg(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n",
483                                 data_len, client->addr, cmd, ret);
484                 else
485                         dev_err(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n",
486                                 data_len, client->addr, cmd, ret);
487         /* 2 transfers must have completed successfully */
488         } else if (ret == 2) {
489                 memcpy(data, buffer, data_len);
490                 ret = 0;
491         } else {
492                 ret = -EIO;
493         }
494
495         kfree(buffer);
496         return ret;
497 }
498
499 static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
500                 u8 cmd, u8 *data, u8 data_len)
501 {
502
503         struct i2c_msg msgs[1];
504         u8 *buffer;
505         int ret = AE_OK;
506
507         buffer = kzalloc(data_len + 1, GFP_KERNEL);
508         if (!buffer)
509                 return AE_NO_MEMORY;
510
511         buffer[0] = cmd;
512         memcpy(buffer + 1, data, data_len);
513
514         msgs[0].addr = client->addr;
515         msgs[0].flags = client->flags;
516         msgs[0].len = data_len + 1;
517         msgs[0].buf = buffer;
518
519         ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
520
521         kfree(buffer);
522
523         if (ret < 0) {
524                 dev_err(&client->adapter->dev, "i2c write failed: %d\n", ret);
525                 return ret;
526         }
527
528         /* 1 transfer must have completed successfully */
529         return (ret == 1) ? 0 : -EIO;
530 }
531
532 static acpi_status
533 i2c_acpi_space_handler(u32 function, acpi_physical_address command,
534                         u32 bits, u64 *value64,
535                         void *handler_context, void *region_context)
536 {
537         struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
538         struct i2c_acpi_handler_data *data = handler_context;
539         struct acpi_connection_info *info = &data->info;
540         struct acpi_resource_i2c_serialbus *sb;
541         struct i2c_adapter *adapter = data->adapter;
542         struct i2c_client *client;
543         struct acpi_resource *ares;
544         u32 accessor_type = function >> 16;
545         u8 action = function & ACPI_IO_MASK;
546         acpi_status ret;
547         int status;
548
549         ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
550         if (ACPI_FAILURE(ret))
551                 return ret;
552
553         client = kzalloc(sizeof(*client), GFP_KERNEL);
554         if (!client) {
555                 ret = AE_NO_MEMORY;
556                 goto err;
557         }
558
559         if (!value64 || !i2c_acpi_get_i2c_resource(ares, &sb)) {
560                 ret = AE_BAD_PARAMETER;
561                 goto err;
562         }
563
564         client->adapter = adapter;
565         client->addr = sb->slave_address;
566
567         if (sb->access_mode == ACPI_I2C_10BIT_MODE)
568                 client->flags |= I2C_CLIENT_TEN;
569
570         switch (accessor_type) {
571         case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
572                 if (action == ACPI_READ) {
573                         status = i2c_smbus_read_byte(client);
574                         if (status >= 0) {
575                                 gsb->bdata = status;
576                                 status = 0;
577                         }
578                 } else {
579                         status = i2c_smbus_write_byte(client, gsb->bdata);
580                 }
581                 break;
582
583         case ACPI_GSB_ACCESS_ATTRIB_BYTE:
584                 if (action == ACPI_READ) {
585                         status = i2c_smbus_read_byte_data(client, command);
586                         if (status >= 0) {
587                                 gsb->bdata = status;
588                                 status = 0;
589                         }
590                 } else {
591                         status = i2c_smbus_write_byte_data(client, command,
592                                         gsb->bdata);
593                 }
594                 break;
595
596         case ACPI_GSB_ACCESS_ATTRIB_WORD:
597                 if (action == ACPI_READ) {
598                         status = i2c_smbus_read_word_data(client, command);
599                         if (status >= 0) {
600                                 gsb->wdata = status;
601                                 status = 0;
602                         }
603                 } else {
604                         status = i2c_smbus_write_word_data(client, command,
605                                         gsb->wdata);
606                 }
607                 break;
608
609         case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
610                 if (action == ACPI_READ) {
611                         status = i2c_smbus_read_block_data(client, command,
612                                         gsb->data);
613                         if (status >= 0) {
614                                 gsb->len = status;
615                                 status = 0;
616                         }
617                 } else {
618                         status = i2c_smbus_write_block_data(client, command,
619                                         gsb->len, gsb->data);
620                 }
621                 break;
622
623         case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
624                 if (action == ACPI_READ) {
625                         status = acpi_gsb_i2c_read_bytes(client, command,
626                                         gsb->data, info->access_length);
627                 } else {
628                         status = acpi_gsb_i2c_write_bytes(client, command,
629                                         gsb->data, info->access_length);
630                 }
631                 break;
632
633         default:
634                 dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
635                          accessor_type, client->addr);
636                 ret = AE_BAD_PARAMETER;
637                 goto err;
638         }
639
640         gsb->status = status;
641
642  err:
643         kfree(client);
644         ACPI_FREE(ares);
645         return ret;
646 }
647
648
649 int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
650 {
651         acpi_handle handle;
652         struct i2c_acpi_handler_data *data;
653         acpi_status status;
654
655         if (!adapter->dev.parent)
656                 return -ENODEV;
657
658         handle = ACPI_HANDLE(adapter->dev.parent);
659
660         if (!handle)
661                 return -ENODEV;
662
663         data = kzalloc(sizeof(struct i2c_acpi_handler_data),
664                             GFP_KERNEL);
665         if (!data)
666                 return -ENOMEM;
667
668         data->adapter = adapter;
669         status = acpi_bus_attach_private_data(handle, (void *)data);
670         if (ACPI_FAILURE(status)) {
671                 kfree(data);
672                 return -ENOMEM;
673         }
674
675         status = acpi_install_address_space_handler(handle,
676                                 ACPI_ADR_SPACE_GSBUS,
677                                 &i2c_acpi_space_handler,
678                                 NULL,
679                                 data);
680         if (ACPI_FAILURE(status)) {
681                 dev_err(&adapter->dev, "Error installing i2c space handler\n");
682                 acpi_bus_detach_private_data(handle);
683                 kfree(data);
684                 return -ENOMEM;
685         }
686
687         acpi_walk_dep_device_list(handle);
688         return 0;
689 }
690
691 void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
692 {
693         acpi_handle handle;
694         struct i2c_acpi_handler_data *data;
695         acpi_status status;
696
697         if (!adapter->dev.parent)
698                 return;
699
700         handle = ACPI_HANDLE(adapter->dev.parent);
701
702         if (!handle)
703                 return;
704
705         acpi_remove_address_space_handler(handle,
706                                 ACPI_ADR_SPACE_GSBUS,
707                                 &i2c_acpi_space_handler);
708
709         status = acpi_bus_get_private_data(handle, (void **)&data);
710         if (ACPI_SUCCESS(status))
711                 kfree(data);
712
713         acpi_bus_detach_private_data(handle);
714 }
715 #endif /* CONFIG_ACPI_I2C_OPREGION */