atmel-flexcom.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for Atmel Flexcom
  4. *
  5. * Copyright (C) 2015 Atmel Corporation
  6. *
  7. * Author: Cyrille Pitchen <[email protected]>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/of.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/err.h>
  16. #include <linux/io.h>
  17. #include <linux/clk.h>
  18. #include <dt-bindings/mfd/atmel-flexcom.h>
  19. /* I/O register offsets */
  20. #define FLEX_MR 0x0 /* Mode Register */
  21. #define FLEX_VERSION 0xfc /* Version Register */
  22. /* Mode Register bit fields */
  23. #define FLEX_MR_OPMODE_OFFSET (0) /* Operating Mode */
  24. #define FLEX_MR_OPMODE_MASK (0x3 << FLEX_MR_OPMODE_OFFSET)
  25. #define FLEX_MR_OPMODE(opmode) (((opmode) << FLEX_MR_OPMODE_OFFSET) & \
  26. FLEX_MR_OPMODE_MASK)
  27. struct atmel_flexcom {
  28. void __iomem *base;
  29. u32 opmode;
  30. struct clk *clk;
  31. };
  32. static int atmel_flexcom_probe(struct platform_device *pdev)
  33. {
  34. struct device_node *np = pdev->dev.of_node;
  35. struct resource *res;
  36. struct atmel_flexcom *ddata;
  37. int err;
  38. ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
  39. if (!ddata)
  40. return -ENOMEM;
  41. platform_set_drvdata(pdev, ddata);
  42. err = of_property_read_u32(np, "atmel,flexcom-mode", &ddata->opmode);
  43. if (err)
  44. return err;
  45. if (ddata->opmode < ATMEL_FLEXCOM_MODE_USART ||
  46. ddata->opmode > ATMEL_FLEXCOM_MODE_TWI)
  47. return -EINVAL;
  48. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  49. ddata->base = devm_ioremap_resource(&pdev->dev, res);
  50. if (IS_ERR(ddata->base))
  51. return PTR_ERR(ddata->base);
  52. ddata->clk = devm_clk_get(&pdev->dev, NULL);
  53. if (IS_ERR(ddata->clk))
  54. return PTR_ERR(ddata->clk);
  55. err = clk_prepare_enable(ddata->clk);
  56. if (err)
  57. return err;
  58. /*
  59. * Set the Operating Mode in the Mode Register: only the selected device
  60. * is clocked. Hence, registers of the other serial devices remain
  61. * inaccessible and are read as zero. Also the external I/O lines of the
  62. * Flexcom are muxed to reach the selected device.
  63. */
  64. writel(FLEX_MR_OPMODE(ddata->opmode), ddata->base + FLEX_MR);
  65. clk_disable_unprepare(ddata->clk);
  66. return devm_of_platform_populate(&pdev->dev);
  67. }
  68. static const struct of_device_id atmel_flexcom_of_match[] = {
  69. { .compatible = "atmel,sama5d2-flexcom" },
  70. { /* sentinel */ }
  71. };
  72. MODULE_DEVICE_TABLE(of, atmel_flexcom_of_match);
  73. static int __maybe_unused atmel_flexcom_resume_noirq(struct device *dev)
  74. {
  75. struct atmel_flexcom *ddata = dev_get_drvdata(dev);
  76. int err;
  77. u32 val;
  78. err = clk_prepare_enable(ddata->clk);
  79. if (err)
  80. return err;
  81. val = FLEX_MR_OPMODE(ddata->opmode),
  82. writel(val, ddata->base + FLEX_MR);
  83. clk_disable_unprepare(ddata->clk);
  84. return 0;
  85. }
  86. static const struct dev_pm_ops __maybe_unused atmel_flexcom_pm_ops = {
  87. .resume_noirq = atmel_flexcom_resume_noirq,
  88. };
  89. static struct platform_driver atmel_flexcom_driver = {
  90. .probe = atmel_flexcom_probe,
  91. .driver = {
  92. .name = "atmel_flexcom",
  93. .pm = pm_ptr(&atmel_flexcom_pm_ops),
  94. .of_match_table = atmel_flexcom_of_match,
  95. },
  96. };
  97. module_platform_driver(atmel_flexcom_driver);
  98. MODULE_AUTHOR("Cyrille Pitchen <[email protected]>");
  99. MODULE_DESCRIPTION("Atmel Flexcom MFD driver");
  100. MODULE_LICENSE("GPL v2");