serial-multi-instantiate.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Serial multi-instantiate driver, pseudo driver to instantiate multiple
  4. * client devices from a single fwnode.
  5. *
  6. * Copyright 2018 Hans de Goede <hdegoede@redhat.com>
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/bits.h>
  10. #include <linux/i2c.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/property.h>
  16. #include <linux/spi/spi.h>
  17. #include <linux/types.h>
  18. #define IRQ_RESOURCE_TYPE GENMASK(1, 0)
  19. #define IRQ_RESOURCE_NONE 0
  20. #define IRQ_RESOURCE_GPIO 1
  21. #define IRQ_RESOURCE_APIC 2
  22. #define IRQ_RESOURCE_AUTO 3
  23. #define IRQ_RESOURCE_OPT BIT(2)
  24. enum smi_bus_type {
  25. SMI_I2C,
  26. SMI_SPI,
  27. SMI_AUTO_DETECT,
  28. };
  29. struct smi_instance {
  30. const char *type;
  31. unsigned int flags;
  32. int irq_idx;
  33. };
  34. struct smi_node {
  35. enum smi_bus_type bus_type;
  36. struct smi_instance instances[];
  37. };
  38. struct smi {
  39. int i2c_num;
  40. int spi_num;
  41. struct i2c_client **i2c_devs;
  42. struct spi_device **spi_devs;
  43. };
  44. static int smi_get_irq(struct platform_device *pdev, struct acpi_device *adev,
  45. const struct smi_instance *inst)
  46. {
  47. int ret;
  48. switch (inst->flags & IRQ_RESOURCE_TYPE) {
  49. case IRQ_RESOURCE_AUTO:
  50. ret = acpi_dev_gpio_irq_get(adev, inst->irq_idx);
  51. if (ret > 0) {
  52. dev_dbg(&pdev->dev, "Using gpio irq\n");
  53. break;
  54. }
  55. ret = platform_get_irq(pdev, inst->irq_idx);
  56. if (ret > 0) {
  57. dev_dbg(&pdev->dev, "Using platform irq\n");
  58. break;
  59. }
  60. if (inst->flags & IRQ_RESOURCE_OPT) {
  61. dev_dbg(&pdev->dev, "No irq\n");
  62. return 0;
  63. }
  64. break;
  65. case IRQ_RESOURCE_GPIO:
  66. ret = acpi_dev_gpio_irq_get(adev, inst->irq_idx);
  67. break;
  68. case IRQ_RESOURCE_APIC:
  69. ret = platform_get_irq(pdev, inst->irq_idx);
  70. break;
  71. default:
  72. return 0;
  73. }
  74. if (ret < 0)
  75. return dev_err_probe(&pdev->dev, ret, "Error requesting irq at index %d\n",
  76. inst->irq_idx);
  77. return ret;
  78. }
  79. static void smi_devs_unregister(struct smi *smi)
  80. {
  81. #if IS_REACHABLE(CONFIG_I2C)
  82. while (smi->i2c_num--)
  83. i2c_unregister_device(smi->i2c_devs[smi->i2c_num]);
  84. #endif
  85. if (IS_REACHABLE(CONFIG_SPI)) {
  86. while (smi->spi_num--)
  87. spi_unregister_device(smi->spi_devs[smi->spi_num]);
  88. }
  89. }
  90. /**
  91. * smi_spi_probe - Instantiate multiple SPI devices from inst array
  92. * @pdev: Platform device
  93. * @smi: Internal struct for Serial multi instantiate driver
  94. * @inst_array: Array of instances to probe
  95. *
  96. * Returns the number of SPI devices instantiate, Zero if none is found or a negative error code.
  97. */
  98. static int smi_spi_probe(struct platform_device *pdev, struct smi *smi,
  99. const struct smi_instance *inst_array)
  100. {
  101. struct device *dev = &pdev->dev;
  102. struct acpi_device *adev = ACPI_COMPANION(dev);
  103. struct spi_controller *ctlr;
  104. struct spi_device *spi_dev;
  105. char name[50];
  106. int i, ret, count;
  107. ret = acpi_spi_count_resources(adev);
  108. if (ret < 0)
  109. return ret;
  110. if (!ret)
  111. return -ENOENT;
  112. count = ret;
  113. smi->spi_devs = devm_kcalloc(dev, count, sizeof(*smi->spi_devs), GFP_KERNEL);
  114. if (!smi->spi_devs)
  115. return -ENOMEM;
  116. for (i = 0; i < count && inst_array[i].type; i++) {
  117. spi_dev = acpi_spi_device_alloc(NULL, adev, i);
  118. if (IS_ERR(spi_dev)) {
  119. ret = dev_err_probe(dev, PTR_ERR(spi_dev), "failed to allocate SPI device %s from ACPI\n",
  120. dev_name(&adev->dev));
  121. goto error;
  122. }
  123. ctlr = spi_dev->controller;
  124. strscpy(spi_dev->modalias, inst_array[i].type);
  125. ret = smi_get_irq(pdev, adev, &inst_array[i]);
  126. if (ret < 0) {
  127. spi_dev_put(spi_dev);
  128. goto error;
  129. }
  130. spi_dev->irq = ret;
  131. snprintf(name, sizeof(name), "%s-%s-%s.%d", dev_name(&ctlr->dev), dev_name(dev),
  132. inst_array[i].type, i);
  133. spi_dev->dev.init_name = name;
  134. ret = spi_add_device(spi_dev);
  135. if (ret) {
  136. dev_err_probe(&ctlr->dev, ret, "failed to add SPI device %s from ACPI\n",
  137. dev_name(&adev->dev));
  138. spi_dev_put(spi_dev);
  139. goto error;
  140. }
  141. dev_dbg(dev, "SPI device %s using chip select %u", name,
  142. spi_get_chipselect(spi_dev, 0));
  143. smi->spi_devs[i] = spi_dev;
  144. smi->spi_num++;
  145. }
  146. if (smi->spi_num < count) {
  147. dev_dbg(dev, "Error finding driver, idx %d\n", i);
  148. ret = -ENODEV;
  149. goto error;
  150. }
  151. dev_info(dev, "Instantiated %d SPI devices.\n", smi->spi_num);
  152. return 0;
  153. error:
  154. smi_devs_unregister(smi);
  155. return ret;
  156. }
  157. /**
  158. * smi_i2c_probe - Instantiate multiple I2C devices from inst array
  159. * @pdev: Platform device
  160. * @smi: Internal struct for Serial multi instantiate driver
  161. * @inst_array: Array of instances to probe
  162. *
  163. * Returns the number of I2C devices instantiate, Zero if none is found or a negative error code.
  164. */
  165. static int smi_i2c_probe(struct platform_device *pdev, struct smi *smi,
  166. const struct smi_instance *inst_array)
  167. {
  168. struct i2c_board_info board_info = {};
  169. struct device *dev = &pdev->dev;
  170. struct acpi_device *adev = ACPI_COMPANION(dev);
  171. char name[32];
  172. int i, ret, count;
  173. ret = i2c_acpi_client_count(adev);
  174. if (ret < 0)
  175. return ret;
  176. if (!ret)
  177. return -ENOENT;
  178. count = ret;
  179. smi->i2c_devs = devm_kcalloc(dev, count, sizeof(*smi->i2c_devs), GFP_KERNEL);
  180. if (!smi->i2c_devs)
  181. return -ENOMEM;
  182. for (i = 0; i < count && inst_array[i].type; i++) {
  183. memset(&board_info, 0, sizeof(board_info));
  184. strscpy(board_info.type, inst_array[i].type);
  185. snprintf(name, sizeof(name), "%s-%s.%d", dev_name(dev), inst_array[i].type, i);
  186. board_info.dev_name = name;
  187. ret = smi_get_irq(pdev, adev, &inst_array[i]);
  188. if (ret < 0)
  189. goto error;
  190. board_info.irq = ret;
  191. smi->i2c_devs[i] = i2c_acpi_new_device(dev, i, &board_info);
  192. if (IS_ERR(smi->i2c_devs[i])) {
  193. ret = dev_err_probe(dev, PTR_ERR(smi->i2c_devs[i]),
  194. "Error creating i2c-client, idx %d\n", i);
  195. goto error;
  196. }
  197. smi->i2c_num++;
  198. }
  199. if (smi->i2c_num < count) {
  200. dev_dbg(dev, "Error finding driver, idx %d\n", i);
  201. ret = -ENODEV;
  202. goto error;
  203. }
  204. dev_info(dev, "Instantiated %d I2C devices.\n", smi->i2c_num);
  205. return 0;
  206. error:
  207. smi_devs_unregister(smi);
  208. return ret;
  209. }
  210. static int smi_probe(struct platform_device *pdev)
  211. {
  212. struct device *dev = &pdev->dev;
  213. const struct smi_node *node;
  214. struct smi *smi;
  215. int ret;
  216. node = device_get_match_data(dev);
  217. if (!node) {
  218. dev_dbg(dev, "Error ACPI match data is missing\n");
  219. return -ENODEV;
  220. }
  221. smi = devm_kzalloc(dev, sizeof(*smi), GFP_KERNEL);
  222. if (!smi)
  223. return -ENOMEM;
  224. platform_set_drvdata(pdev, smi);
  225. switch (node->bus_type) {
  226. case SMI_I2C:
  227. if (IS_REACHABLE(CONFIG_I2C))
  228. return smi_i2c_probe(pdev, smi, node->instances);
  229. return -ENODEV;
  230. case SMI_SPI:
  231. if (IS_REACHABLE(CONFIG_SPI))
  232. return smi_spi_probe(pdev, smi, node->instances);
  233. return -ENODEV;
  234. case SMI_AUTO_DETECT:
  235. /*
  236. * For backwards-compatibility with the existing nodes I2C
  237. * is checked first and if such entries are found ONLY I2C
  238. * devices are created. Since some existing nodes that were
  239. * already handled by this driver could also contain unrelated
  240. * SpiSerialBus nodes that were previously ignored, and this
  241. * preserves that behavior.
  242. */
  243. if (IS_REACHABLE(CONFIG_I2C)) {
  244. ret = smi_i2c_probe(pdev, smi, node->instances);
  245. if (ret != -ENOENT)
  246. return ret;
  247. }
  248. if (IS_REACHABLE(CONFIG_SPI))
  249. return smi_spi_probe(pdev, smi, node->instances);
  250. return -ENODEV;
  251. default:
  252. return -EINVAL;
  253. }
  254. }
  255. static void smi_remove(struct platform_device *pdev)
  256. {
  257. struct smi *smi = platform_get_drvdata(pdev);
  258. smi_devs_unregister(smi);
  259. }
  260. static const struct smi_node bsg1160_data = {
  261. .instances = {
  262. { "bmc150_accel", IRQ_RESOURCE_GPIO, 0 },
  263. { "bmc150_magn" },
  264. { "bmg160" },
  265. {}
  266. },
  267. .bus_type = SMI_I2C,
  268. };
  269. static const struct smi_node bsg2150_data = {
  270. .instances = {
  271. { "bmc150_accel", IRQ_RESOURCE_GPIO, 0 },
  272. { "bmc150_magn" },
  273. /* The resources describe a 3th client, but it is not really there. */
  274. { "bsg2150_dummy_dev" },
  275. {}
  276. },
  277. .bus_type = SMI_I2C,
  278. };
  279. static const struct smi_node int3515_data = {
  280. .instances = {
  281. { "tps6598x", IRQ_RESOURCE_APIC, 0 },
  282. { "tps6598x", IRQ_RESOURCE_APIC, 1 },
  283. { "tps6598x", IRQ_RESOURCE_APIC, 2 },
  284. { "tps6598x", IRQ_RESOURCE_APIC, 3 },
  285. {}
  286. },
  287. .bus_type = SMI_I2C,
  288. };
  289. static const struct smi_node cs35l41_hda = {
  290. .instances = {
  291. { "cs35l41-hda", IRQ_RESOURCE_AUTO, 0 },
  292. { "cs35l41-hda", IRQ_RESOURCE_AUTO, 0 },
  293. { "cs35l41-hda", IRQ_RESOURCE_AUTO, 0 },
  294. { "cs35l41-hda", IRQ_RESOURCE_AUTO, 0 },
  295. {}
  296. },
  297. .bus_type = SMI_AUTO_DETECT,
  298. };
  299. static const struct smi_node cs35l54_hda = {
  300. .instances = {
  301. { "cs35l54-hda", IRQ_RESOURCE_AUTO, 0 },
  302. { "cs35l54-hda", IRQ_RESOURCE_AUTO, 0 },
  303. { "cs35l54-hda", IRQ_RESOURCE_AUTO, 0 },
  304. { "cs35l54-hda", IRQ_RESOURCE_AUTO, 0 },
  305. /* a 5th entry is an alias address, not a real device */
  306. { "cs35l54-hda_dummy_dev" },
  307. {}
  308. },
  309. .bus_type = SMI_AUTO_DETECT,
  310. };
  311. static const struct smi_node cs35l56_hda = {
  312. .instances = {
  313. { "cs35l56-hda", IRQ_RESOURCE_AUTO, 0 },
  314. { "cs35l56-hda", IRQ_RESOURCE_AUTO, 0 },
  315. { "cs35l56-hda", IRQ_RESOURCE_AUTO, 0 },
  316. { "cs35l56-hda", IRQ_RESOURCE_AUTO, 0 },
  317. /* a 5th entry is an alias address, not a real device */
  318. { "cs35l56-hda_dummy_dev" },
  319. {}
  320. },
  321. .bus_type = SMI_AUTO_DETECT,
  322. };
  323. static const struct smi_node cs35l57_hda = {
  324. .instances = {
  325. { "cs35l57-hda", IRQ_RESOURCE_AUTO, 0 },
  326. { "cs35l57-hda", IRQ_RESOURCE_AUTO, 0 },
  327. { "cs35l57-hda", IRQ_RESOURCE_AUTO, 0 },
  328. { "cs35l57-hda", IRQ_RESOURCE_AUTO, 0 },
  329. /* a 5th entry is an alias address, not a real device */
  330. { "cs35l57-hda_dummy_dev" },
  331. {}
  332. },
  333. .bus_type = SMI_AUTO_DETECT,
  334. };
  335. static const struct smi_node tas2781_hda = {
  336. .instances = {
  337. { "tas2781-hda", IRQ_RESOURCE_AUTO | IRQ_RESOURCE_OPT, 0 },
  338. { "tas2781-hda", IRQ_RESOURCE_AUTO | IRQ_RESOURCE_OPT, 0 },
  339. { "tas2781-hda", IRQ_RESOURCE_AUTO | IRQ_RESOURCE_OPT, 0 },
  340. { "tas2781-hda", IRQ_RESOURCE_AUTO | IRQ_RESOURCE_OPT, 0 },
  341. {}
  342. },
  343. .bus_type = SMI_AUTO_DETECT,
  344. };
  345. /*
  346. * Note new device-ids must also be added to ignore_serial_bus_ids in
  347. * drivers/acpi/scan.c: acpi_device_enumeration_by_parent().
  348. */
  349. static const struct acpi_device_id smi_acpi_ids[] = {
  350. { "BSG1160", (unsigned long)&bsg1160_data },
  351. { "BSG2150", (unsigned long)&bsg2150_data },
  352. { "CSC3551", (unsigned long)&cs35l41_hda },
  353. { "CSC3554", (unsigned long)&cs35l54_hda },
  354. { "CSC3556", (unsigned long)&cs35l56_hda },
  355. { "CSC3557", (unsigned long)&cs35l57_hda },
  356. { "INT3515", (unsigned long)&int3515_data },
  357. { "TXNW2781", (unsigned long)&tas2781_hda },
  358. /* Non-conforming _HID for Cirrus Logic already released */
  359. { "CLSA0100", (unsigned long)&cs35l41_hda },
  360. { "CLSA0101", (unsigned long)&cs35l41_hda },
  361. { }
  362. };
  363. MODULE_DEVICE_TABLE(acpi, smi_acpi_ids);
  364. static struct platform_driver smi_driver = {
  365. .driver = {
  366. .name = "Serial bus multi instantiate pseudo device driver",
  367. .acpi_match_table = smi_acpi_ids,
  368. },
  369. .probe = smi_probe,
  370. .remove = smi_remove,
  371. };
  372. module_platform_driver(smi_driver);
  373. MODULE_DESCRIPTION("Serial multi instantiate pseudo device driver");
  374. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  375. MODULE_LICENSE("GPL");