^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * I2C multi-instantiate driver, pseudo driver to instantiate multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * i2c-clients from a single fwnode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright 2018 Hans de Goede <hdegoede@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/bits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define IRQ_RESOURCE_TYPE GENMASK(1, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define IRQ_RESOURCE_NONE 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define IRQ_RESOURCE_GPIO 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define IRQ_RESOURCE_APIC 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct i2c_inst_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) const char *type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) unsigned int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int irq_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct i2c_multi_inst_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int num_clients;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct i2c_client *clients[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static int i2c_multi_inst_count(struct acpi_resource *ares, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct acpi_resource_i2c_serialbus *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) int *count = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (i2c_acpi_get_i2c_resource(ares, &sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) *count = *count + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static int i2c_multi_inst_count_resources(struct acpi_device *adev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) LIST_HEAD(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) ret = acpi_dev_get_resources(adev, &r, i2c_multi_inst_count, &count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) acpi_dev_free_resource_list(&r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static int i2c_multi_inst_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct i2c_multi_inst_data *multi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) const struct acpi_device_id *match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) const struct i2c_inst_data *inst_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct i2c_board_info board_info = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct acpi_device *adev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) char name[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) match = acpi_match_device(dev->driver->acpi_match_table, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (!match) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) dev_err(dev, "Error ACPI match data is missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) inst_data = (const struct i2c_inst_data *)match->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) adev = ACPI_COMPANION(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /* Count number of clients to instantiate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) ret = i2c_multi_inst_count_resources(adev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) multi = devm_kmalloc(dev, struct_size(multi, clients, ret), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (!multi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) multi->num_clients = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) for (i = 0; i < multi->num_clients && inst_data[i].type; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) memset(&board_info, 0, sizeof(board_info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) strlcpy(board_info.type, inst_data[i].type, I2C_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) snprintf(name, sizeof(name), "%s-%s.%d", dev_name(dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) inst_data[i].type, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) board_info.dev_name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) switch (inst_data[i].flags & IRQ_RESOURCE_TYPE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) case IRQ_RESOURCE_GPIO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) ret = acpi_dev_gpio_irq_get(adev, inst_data[i].irq_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) dev_err(dev, "Error requesting irq at index %d: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) inst_data[i].irq_idx, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) board_info.irq = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) case IRQ_RESOURCE_APIC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) ret = platform_get_irq(pdev, inst_data[i].irq_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) dev_dbg(dev, "Error requesting irq at index %d: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) inst_data[i].irq_idx, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) board_info.irq = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) board_info.irq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) multi->clients[i] = i2c_acpi_new_device(dev, i, &board_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (IS_ERR(multi->clients[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) ret = PTR_ERR(multi->clients[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (ret != -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) dev_err(dev, "Error creating i2c-client, idx %d\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (i < multi->num_clients) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) dev_err(dev, "Error finding driver, idx %d\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) platform_set_drvdata(pdev, multi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) while (--i >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) i2c_unregister_device(multi->clients[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static int i2c_multi_inst_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) struct i2c_multi_inst_data *multi = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) for (i = 0; i < multi->num_clients; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) i2c_unregister_device(multi->clients[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static const struct i2c_inst_data bsg1160_data[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) { "bmc150_accel", IRQ_RESOURCE_GPIO, 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) { "bmc150_magn" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) { "bmg160" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static const struct i2c_inst_data bsg2150_data[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) { "bmc150_accel", IRQ_RESOURCE_GPIO, 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) { "bmc150_magn" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /* The resources describe a 3th client, but it is not really there. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) { "bsg2150_dummy_dev" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * Device with _HID INT3515 (TI PD controllers) has some unresolved interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * issues. The most common problem seen is interrupt flood.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * There are at least two known causes. Firstly, on some boards, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * I2CSerialBus resource index does not match the Interrupt resource, i.e. they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * are not one-to-one mapped like in the array below. Secondly, on some boards
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * the IRQ line from the PD controller is not actually connected at all. But the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * interrupt flood is also seen on some boards where those are not a problem, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * there are some other problems as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * Because of the issues with the interrupt, the device is disabled for now. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * you wish to debug the issues, uncomment the below, and add an entry for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * INT3515 device to the i2c_multi_instance_ids table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * static const struct i2c_inst_data int3515_data[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * { "tps6598x", IRQ_RESOURCE_APIC, 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * { "tps6598x", IRQ_RESOURCE_APIC, 1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * { "tps6598x", IRQ_RESOURCE_APIC, 2 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * { "tps6598x", IRQ_RESOURCE_APIC, 3 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * Note new device-ids must also be added to i2c_multi_instantiate_ids in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * drivers/acpi/scan.c: acpi_device_enumeration_by_parent().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static const struct acpi_device_id i2c_multi_inst_acpi_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) { "BSG1160", (unsigned long)bsg1160_data },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) { "BSG2150", (unsigned long)bsg2150_data },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) MODULE_DEVICE_TABLE(acpi, i2c_multi_inst_acpi_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static struct platform_driver i2c_multi_inst_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) .name = "I2C multi instantiate pseudo device driver",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) .acpi_match_table = ACPI_PTR(i2c_multi_inst_acpi_ids),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) .probe = i2c_multi_inst_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) .remove = i2c_multi_inst_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) module_platform_driver(i2c_multi_inst_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) MODULE_DESCRIPTION("I2C multi instantiate pseudo device driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) MODULE_LICENSE("GPL");