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