Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * STM32 Low-Power Timer parent driver.
  4 * Copyright (C) STMicroelectronics 2017
  5 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>
  6 * Inspired by Benjamin Gaignard's stm32-timers driver
  7 */
  8
  9#include <linux/mfd/stm32-lptimer.h>
 10#include <linux/module.h>
 11#include <linux/of_platform.h>
 12
 13#define STM32_LPTIM_MAX_REGISTER	0x3fc
 14
 15static const struct regmap_config stm32_lptimer_regmap_cfg = {
 16	.reg_bits = 32,
 17	.val_bits = 32,
 18	.reg_stride = sizeof(u32),
 19	.max_register = STM32_LPTIM_MAX_REGISTER,
 20	.fast_io = true,
 21};
 22
 23static int stm32_lptimer_detect_encoder(struct stm32_lptimer *ddata)
 24{
 25	u32 val;
 26	int ret;
 27
 28	/*
 29	 * Quadrature encoder mode bit can only be written and read back when
 30	 * Low-Power Timer supports it.
 31	 */
 32	ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
 33				 STM32_LPTIM_ENC, STM32_LPTIM_ENC);
 34	if (ret)
 35		return ret;
 36
 37	ret = regmap_read(ddata->regmap, STM32_LPTIM_CFGR, &val);
 38	if (ret)
 39		return ret;
 40
 41	ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
 42				 STM32_LPTIM_ENC, 0);
 43	if (ret)
 44		return ret;
 45
 46	ddata->has_encoder = !!(val & STM32_LPTIM_ENC);
 47
 48	return 0;
 49}
 50
 51static int stm32_lptimer_probe(struct platform_device *pdev)
 52{
 53	struct device *dev = &pdev->dev;
 54	struct stm32_lptimer *ddata;
 55	struct resource *res;
 56	void __iomem *mmio;
 57	int ret;
 58
 59	ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
 60	if (!ddata)
 61		return -ENOMEM;
 62
 63	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 64	mmio = devm_ioremap_resource(dev, res);
 65	if (IS_ERR(mmio))
 66		return PTR_ERR(mmio);
 67
 68	ddata->regmap = devm_regmap_init_mmio_clk(dev, "mux", mmio,
 69						  &stm32_lptimer_regmap_cfg);
 70	if (IS_ERR(ddata->regmap))
 71		return PTR_ERR(ddata->regmap);
 72
 73	ddata->clk = devm_clk_get(dev, NULL);
 74	if (IS_ERR(ddata->clk))
 75		return PTR_ERR(ddata->clk);
 76
 77	ret = stm32_lptimer_detect_encoder(ddata);
 78	if (ret)
 79		return ret;
 80
 81	platform_set_drvdata(pdev, ddata);
 82
 83	return devm_of_platform_populate(&pdev->dev);
 84}
 85
 86static const struct of_device_id stm32_lptimer_of_match[] = {
 87	{ .compatible = "st,stm32-lptimer", },
 88	{},
 89};
 90MODULE_DEVICE_TABLE(of, stm32_lptimer_of_match);
 91
 92static struct platform_driver stm32_lptimer_driver = {
 93	.probe = stm32_lptimer_probe,
 94	.driver = {
 95		.name = "stm32-lptimer",
 96		.of_match_table = stm32_lptimer_of_match,
 97	},
 98};
 99module_platform_driver(stm32_lptimer_driver);
100
101MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
102MODULE_DESCRIPTION("STMicroelectronics STM32 Low-Power Timer");
103MODULE_ALIAS("platform:stm32-lptimer");
104MODULE_LICENSE("GPL v2");
v5.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * STM32 Low-Power Timer parent driver.
  4 * Copyright (C) STMicroelectronics 2017
  5 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>
  6 * Inspired by Benjamin Gaignard's stm32-timers driver
  7 */
  8
  9#include <linux/mfd/stm32-lptimer.h>
 10#include <linux/module.h>
 11#include <linux/of_platform.h>
 12
 13#define STM32_LPTIM_MAX_REGISTER	0x3fc
 14
 15static const struct regmap_config stm32_lptimer_regmap_cfg = {
 16	.reg_bits = 32,
 17	.val_bits = 32,
 18	.reg_stride = sizeof(u32),
 19	.max_register = STM32_LPTIM_MAX_REGISTER,
 
 20};
 21
 22static int stm32_lptimer_detect_encoder(struct stm32_lptimer *ddata)
 23{
 24	u32 val;
 25	int ret;
 26
 27	/*
 28	 * Quadrature encoder mode bit can only be written and read back when
 29	 * Low-Power Timer supports it.
 30	 */
 31	ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
 32				 STM32_LPTIM_ENC, STM32_LPTIM_ENC);
 33	if (ret)
 34		return ret;
 35
 36	ret = regmap_read(ddata->regmap, STM32_LPTIM_CFGR, &val);
 37	if (ret)
 38		return ret;
 39
 40	ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
 41				 STM32_LPTIM_ENC, 0);
 42	if (ret)
 43		return ret;
 44
 45	ddata->has_encoder = !!(val & STM32_LPTIM_ENC);
 46
 47	return 0;
 48}
 49
 50static int stm32_lptimer_probe(struct platform_device *pdev)
 51{
 52	struct device *dev = &pdev->dev;
 53	struct stm32_lptimer *ddata;
 54	struct resource *res;
 55	void __iomem *mmio;
 56	int ret;
 57
 58	ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
 59	if (!ddata)
 60		return -ENOMEM;
 61
 62	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 63	mmio = devm_ioremap_resource(dev, res);
 64	if (IS_ERR(mmio))
 65		return PTR_ERR(mmio);
 66
 67	ddata->regmap = devm_regmap_init_mmio_clk(dev, "mux", mmio,
 68						  &stm32_lptimer_regmap_cfg);
 69	if (IS_ERR(ddata->regmap))
 70		return PTR_ERR(ddata->regmap);
 71
 72	ddata->clk = devm_clk_get(dev, NULL);
 73	if (IS_ERR(ddata->clk))
 74		return PTR_ERR(ddata->clk);
 75
 76	ret = stm32_lptimer_detect_encoder(ddata);
 77	if (ret)
 78		return ret;
 79
 80	platform_set_drvdata(pdev, ddata);
 81
 82	return devm_of_platform_populate(&pdev->dev);
 83}
 84
 85static const struct of_device_id stm32_lptimer_of_match[] = {
 86	{ .compatible = "st,stm32-lptimer", },
 87	{},
 88};
 89MODULE_DEVICE_TABLE(of, stm32_lptimer_of_match);
 90
 91static struct platform_driver stm32_lptimer_driver = {
 92	.probe = stm32_lptimer_probe,
 93	.driver = {
 94		.name = "stm32-lptimer",
 95		.of_match_table = stm32_lptimer_of_match,
 96	},
 97};
 98module_platform_driver(stm32_lptimer_driver);
 99
100MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
101MODULE_DESCRIPTION("STMicroelectronics STM32 Low-Power Timer");
102MODULE_ALIAS("platform:stm32-lptimer");
103MODULE_LICENSE("GPL v2");