8250_bcm2835aux.c 3.5 KB

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