8250_bcm2835aux.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Serial port driver for BCM2835AUX UART
  4. *
  5. * Copyright (C) 2016 Martin Sperl <kernel@martin.sperl.org>
  6. *
  7. * Based on 8250_lpc18xx.c:
  8. * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/io.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include "8250.h"
  16. struct bcm2835aux_data {
  17. struct uart_8250_port uart;
  18. struct clk *clk;
  19. int line;
  20. };
  21. static int bcm2835aux_serial_probe(struct platform_device *pdev)
  22. {
  23. struct bcm2835aux_data *data;
  24. struct resource *res;
  25. int ret;
  26. /* allocate the custom structure */
  27. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  28. if (!data)
  29. return -ENOMEM;
  30. /* initialize data */
  31. spin_lock_init(&data->uart.port.lock);
  32. data->uart.capabilities = UART_CAP_FIFO | UART_CAP_MINI;
  33. data->uart.port.dev = &pdev->dev;
  34. data->uart.port.regshift = 2;
  35. data->uart.port.type = PORT_16550;
  36. data->uart.port.iotype = UPIO_MEM;
  37. data->uart.port.fifosize = 8;
  38. data->uart.port.flags = UPF_SHARE_IRQ |
  39. UPF_FIXED_PORT |
  40. UPF_FIXED_TYPE |
  41. UPF_SKIP_TEST;
  42. /* get the clock - this also enables the HW */
  43. data->clk = devm_clk_get(&pdev->dev, NULL);
  44. ret = PTR_ERR_OR_ZERO(data->clk);
  45. if (ret) {
  46. dev_err(&pdev->dev, "could not get clk: %d\n", ret);
  47. return ret;
  48. }
  49. /* get the interrupt */
  50. ret = platform_get_irq(pdev, 0);
  51. if (ret < 0)
  52. return ret;
  53. data->uart.port.irq = ret;
  54. /* map the main registers */
  55. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  56. if (!res) {
  57. dev_err(&pdev->dev, "memory resource not found");
  58. return -EINVAL;
  59. }
  60. data->uart.port.membase = devm_ioremap_resource(&pdev->dev, res);
  61. ret = PTR_ERR_OR_ZERO(data->uart.port.membase);
  62. if (ret)
  63. return ret;
  64. /* Check for a fixed line number */
  65. ret = of_alias_get_id(pdev->dev.of_node, "serial");
  66. if (ret >= 0)
  67. data->uart.port.line = ret;
  68. /* enable the clock as a last step */
  69. ret = clk_prepare_enable(data->clk);
  70. if (ret) {
  71. dev_err(&pdev->dev, "unable to enable uart clock - %d\n",
  72. ret);
  73. return ret;
  74. }
  75. /* the HW-clock divider for bcm2835aux is 8,
  76. * but 8250 expects a divider of 16,
  77. * so we have to multiply the actual clock by 2
  78. * to get identical baudrates.
  79. */
  80. data->uart.port.uartclk = clk_get_rate(data->clk) * 2;
  81. /* register the port */
  82. ret = serial8250_register_8250_port(&data->uart);
  83. if (ret < 0) {
  84. dev_err(&pdev->dev, "unable to register 8250 port - %d\n",
  85. ret);
  86. goto dis_clk;
  87. }
  88. data->line = ret;
  89. platform_set_drvdata(pdev, data);
  90. return 0;
  91. dis_clk:
  92. clk_disable_unprepare(data->clk);
  93. return ret;
  94. }
  95. static int bcm2835aux_serial_remove(struct platform_device *pdev)
  96. {
  97. struct bcm2835aux_data *data = platform_get_drvdata(pdev);
  98. serial8250_unregister_port(data->line);
  99. clk_disable_unprepare(data->clk);
  100. return 0;
  101. }
  102. static const struct of_device_id bcm2835aux_serial_match[] = {
  103. { .compatible = "brcm,bcm2835-aux-uart" },
  104. { },
  105. };
  106. MODULE_DEVICE_TABLE(of, bcm2835aux_serial_match);
  107. static struct platform_driver bcm2835aux_serial_driver = {
  108. .driver = {
  109. .name = "bcm2835-aux-uart",
  110. .of_match_table = bcm2835aux_serial_match,
  111. },
  112. .probe = bcm2835aux_serial_probe,
  113. .remove = bcm2835aux_serial_remove,
  114. };
  115. module_platform_driver(bcm2835aux_serial_driver);
  116. MODULE_DESCRIPTION("BCM2835 auxiliar UART driver");
  117. MODULE_AUTHOR("Martin Sperl <kernel@martin.sperl.org>");
  118. MODULE_LICENSE("GPL v2");