1 /*
2  * STM32 Low-Power Timer Trigger driver
3  *
4  * Copyright (C) STMicroelectronics 2017
5  *
6  * Author: Fabrice Gasnier <fabrice.gasnier@st.com>.
7  *
8  * License terms:  GNU General Public License (GPL), version 2
9  *
10  * Inspired by Benjamin Gaignard's stm32-timer-trigger driver
11  */
12 
13 #include <linux/iio/timer/stm32-lptim-trigger.h>
14 #include <linux/mfd/stm32-lptimer.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 
18 /* List Low-Power Timer triggers */
19 static const char * const stm32_lptim_triggers[] = {
20 	LPTIM1_OUT,
21 	LPTIM2_OUT,
22 	LPTIM3_OUT,
23 };
24 
25 struct stm32_lptim_trigger {
26 	struct device *dev;
27 	const char *trg;
28 };
29 
30 static int stm32_lptim_validate_device(struct iio_trigger *trig,
31 				       struct iio_dev *indio_dev)
32 {
33 	if (indio_dev->modes & INDIO_HARDWARE_TRIGGERED)
34 		return 0;
35 
36 	return -EINVAL;
37 }
38 
39 static const struct iio_trigger_ops stm32_lptim_trigger_ops = {
40 	.validate_device = stm32_lptim_validate_device,
41 };
42 
43 /**
44  * is_stm32_lptim_trigger
45  * @trig: trigger to be checked
46  *
47  * return true if the trigger is a valid STM32 IIO Low-Power Timer Trigger
48  * either return false
49  */
50 bool is_stm32_lptim_trigger(struct iio_trigger *trig)
51 {
52 	return (trig->ops == &stm32_lptim_trigger_ops);
53 }
54 EXPORT_SYMBOL(is_stm32_lptim_trigger);
55 
56 static int stm32_lptim_setup_trig(struct stm32_lptim_trigger *priv)
57 {
58 	struct iio_trigger *trig;
59 
60 	trig = devm_iio_trigger_alloc(priv->dev, "%s", priv->trg);
61 	if  (!trig)
62 		return -ENOMEM;
63 
64 	trig->dev.parent = priv->dev->parent;
65 	trig->ops = &stm32_lptim_trigger_ops;
66 	iio_trigger_set_drvdata(trig, priv);
67 
68 	return devm_iio_trigger_register(priv->dev, trig);
69 }
70 
71 static int stm32_lptim_trigger_probe(struct platform_device *pdev)
72 {
73 	struct stm32_lptim_trigger *priv;
74 	u32 index;
75 	int ret;
76 
77 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
78 	if (!priv)
79 		return -ENOMEM;
80 
81 	if (of_property_read_u32(pdev->dev.of_node, "reg", &index))
82 		return -EINVAL;
83 
84 	if (index >= ARRAY_SIZE(stm32_lptim_triggers))
85 		return -EINVAL;
86 
87 	priv->dev = &pdev->dev;
88 	priv->trg = stm32_lptim_triggers[index];
89 
90 	ret = stm32_lptim_setup_trig(priv);
91 	if (ret)
92 		return ret;
93 
94 	platform_set_drvdata(pdev, priv);
95 
96 	return 0;
97 }
98 
99 static const struct of_device_id stm32_lptim_trig_of_match[] = {
100 	{ .compatible = "st,stm32-lptimer-trigger", },
101 	{},
102 };
103 MODULE_DEVICE_TABLE(of, stm32_lptim_trig_of_match);
104 
105 static struct platform_driver stm32_lptim_trigger_driver = {
106 	.probe = stm32_lptim_trigger_probe,
107 	.driver = {
108 		.name = "stm32-lptimer-trigger",
109 		.of_match_table = stm32_lptim_trig_of_match,
110 	},
111 };
112 module_platform_driver(stm32_lptim_trigger_driver);
113 
114 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
115 MODULE_ALIAS("platform:stm32-lptimer-trigger");
116 MODULE_DESCRIPTION("STMicroelectronics STM32 LPTIM trigger driver");
117 MODULE_LICENSE("GPL v2");
118