atmel-flexcom.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Driver for Atmel Flexcom
  3. *
  4. * Copyright (C) 2015 Atmel Corporation
  5. *
  6. * Author: Cyrille Pitchen <cyrille.pitchen@atmel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/types.h>
  22. #include <linux/kernel.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/of.h>
  25. #include <linux/of_platform.h>
  26. #include <linux/err.h>
  27. #include <linux/io.h>
  28. #include <linux/clk.h>
  29. #include <dt-bindings/mfd/atmel-flexcom.h>
  30. /* I/O register offsets */
  31. #define FLEX_MR 0x0 /* Mode Register */
  32. #define FLEX_VERSION 0xfc /* Version Register */
  33. /* Mode Register bit fields */
  34. #define FLEX_MR_OPMODE_OFFSET (0) /* Operating Mode */
  35. #define FLEX_MR_OPMODE_MASK (0x3 << FLEX_MR_OPMODE_OFFSET)
  36. #define FLEX_MR_OPMODE(opmode) (((opmode) << FLEX_MR_OPMODE_OFFSET) & \
  37. FLEX_MR_OPMODE_MASK)
  38. static int atmel_flexcom_probe(struct platform_device *pdev)
  39. {
  40. struct device_node *np = pdev->dev.of_node;
  41. struct clk *clk;
  42. struct resource *res;
  43. void __iomem *base;
  44. u32 opmode;
  45. int err;
  46. err = of_property_read_u32(np, "atmel,flexcom-mode", &opmode);
  47. if (err)
  48. return err;
  49. if (opmode < ATMEL_FLEXCOM_MODE_USART ||
  50. opmode > ATMEL_FLEXCOM_MODE_TWI)
  51. return -EINVAL;
  52. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  53. base = devm_ioremap_resource(&pdev->dev, res);
  54. if (IS_ERR(base))
  55. return PTR_ERR(base);
  56. clk = devm_clk_get(&pdev->dev, NULL);
  57. if (IS_ERR(clk))
  58. return PTR_ERR(clk);
  59. err = clk_prepare_enable(clk);
  60. if (err)
  61. return err;
  62. /*
  63. * Set the Operating Mode in the Mode Register: only the selected device
  64. * is clocked. Hence, registers of the other serial devices remain
  65. * inaccessible and are read as zero. Also the external I/O lines of the
  66. * Flexcom are muxed to reach the selected device.
  67. */
  68. writel(FLEX_MR_OPMODE(opmode), base + FLEX_MR);
  69. clk_disable_unprepare(clk);
  70. return of_platform_populate(np, NULL, NULL, &pdev->dev);
  71. }
  72. static const struct of_device_id atmel_flexcom_of_match[] = {
  73. { .compatible = "atmel,sama5d2-flexcom" },
  74. { /* sentinel */ }
  75. };
  76. MODULE_DEVICE_TABLE(of, atmel_flexcom_of_match);
  77. static struct platform_driver atmel_flexcom_driver = {
  78. .probe = atmel_flexcom_probe,
  79. .driver = {
  80. .name = "atmel_flexcom",
  81. .of_match_table = atmel_flexcom_of_match,
  82. },
  83. };
  84. module_platform_driver(atmel_flexcom_driver);
  85. MODULE_AUTHOR("Cyrille Pitchen <cyrille.pitchen@atmel.com>");
  86. MODULE_DESCRIPTION("Atmel Flexcom MFD driver");
  87. MODULE_LICENSE("GPL v2");