atmel-flexcom.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. struct atmel_flexcom {
  39. void __iomem *base;
  40. u32 opmode;
  41. struct clk *clk;
  42. };
  43. static int atmel_flexcom_probe(struct platform_device *pdev)
  44. {
  45. struct device_node *np = pdev->dev.of_node;
  46. struct resource *res;
  47. struct atmel_flexcom *ddata;
  48. int err;
  49. ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
  50. if (!ddata)
  51. return -ENOMEM;
  52. platform_set_drvdata(pdev, ddata);
  53. err = of_property_read_u32(np, "atmel,flexcom-mode", &ddata->opmode);
  54. if (err)
  55. return err;
  56. if (ddata->opmode < ATMEL_FLEXCOM_MODE_USART ||
  57. ddata->opmode > ATMEL_FLEXCOM_MODE_TWI)
  58. return -EINVAL;
  59. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  60. ddata->base = devm_ioremap_resource(&pdev->dev, res);
  61. if (IS_ERR(ddata->base))
  62. return PTR_ERR(ddata->base);
  63. ddata->clk = devm_clk_get(&pdev->dev, NULL);
  64. if (IS_ERR(ddata->clk))
  65. return PTR_ERR(ddata->clk);
  66. err = clk_prepare_enable(ddata->clk);
  67. if (err)
  68. return err;
  69. /*
  70. * Set the Operating Mode in the Mode Register: only the selected device
  71. * is clocked. Hence, registers of the other serial devices remain
  72. * inaccessible and are read as zero. Also the external I/O lines of the
  73. * Flexcom are muxed to reach the selected device.
  74. */
  75. writel(FLEX_MR_OPMODE(ddata->opmode), ddata->base + FLEX_MR);
  76. clk_disable_unprepare(ddata->clk);
  77. return devm_of_platform_populate(&pdev->dev);
  78. }
  79. static const struct of_device_id atmel_flexcom_of_match[] = {
  80. { .compatible = "atmel,sama5d2-flexcom" },
  81. { /* sentinel */ }
  82. };
  83. MODULE_DEVICE_TABLE(of, atmel_flexcom_of_match);
  84. #ifdef CONFIG_PM_SLEEP
  85. static int atmel_flexcom_resume(struct device *dev)
  86. {
  87. struct atmel_flexcom *ddata = dev_get_drvdata(dev);
  88. int err;
  89. u32 val;
  90. err = clk_prepare_enable(ddata->clk);
  91. if (err)
  92. return err;
  93. val = FLEX_MR_OPMODE(ddata->opmode),
  94. writel(val, ddata->base + FLEX_MR);
  95. clk_disable_unprepare(ddata->clk);
  96. return 0;
  97. }
  98. #endif
  99. static SIMPLE_DEV_PM_OPS(atmel_flexcom_pm_ops, NULL,
  100. atmel_flexcom_resume);
  101. static struct platform_driver atmel_flexcom_driver = {
  102. .probe = atmel_flexcom_probe,
  103. .driver = {
  104. .name = "atmel_flexcom",
  105. .pm = &atmel_flexcom_pm_ops,
  106. .of_match_table = atmel_flexcom_of_match,
  107. },
  108. };
  109. module_platform_driver(atmel_flexcom_driver);
  110. MODULE_AUTHOR("Cyrille Pitchen <cyrille.pitchen@atmel.com>");
  111. MODULE_DESCRIPTION("Atmel Flexcom MFD driver");
  112. MODULE_LICENSE("GPL v2");