xref: /linux/drivers/input/misc/regulator-haptic.c (revision 4dd10fbbeb9c33709edc89916e19c03c780a0751)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Regulator haptic driver
4  *
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  * Author: Jaewon Kim <jaewon02.kim@samsung.com>
7  * Author: Hyunhee Kim <hyunhee.kim@samsung.com>
8  */
9 
10 #include <linux/input.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/platform_data/regulator-haptic.h>
14 #include <linux/platform_device.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/slab.h>
17 #include <linux/string_choices.h>
18 
19 #define MAX_MAGNITUDE_SHIFT	16
20 
21 struct regulator_haptic {
22 	struct device *dev;
23 	struct input_dev *input_dev;
24 	struct regulator *regulator;
25 
26 	struct work_struct work;
27 	struct mutex mutex;
28 
29 	bool active;
30 	bool suspended;
31 
32 	unsigned int max_volt;
33 	unsigned int min_volt;
34 	unsigned int magnitude;
35 };
36 
37 static int regulator_haptic_toggle(struct regulator_haptic *haptic, bool on)
38 {
39 	int error;
40 
41 	if (haptic->active != on) {
42 
43 		error = on ? regulator_enable(haptic->regulator) :
44 			     regulator_disable(haptic->regulator);
45 		if (error) {
46 			dev_err(haptic->dev,
47 				"failed to switch regulator %s: %d\n",
48 				str_on_off(on), error);
49 			return error;
50 		}
51 
52 		haptic->active = on;
53 	}
54 
55 	return 0;
56 }
57 
58 static int regulator_haptic_set_voltage(struct regulator_haptic *haptic,
59 					 unsigned int magnitude)
60 {
61 	u64 volt_mag_multi;
62 	unsigned int intensity;
63 	int error;
64 
65 	volt_mag_multi = (u64)(haptic->max_volt - haptic->min_volt) * magnitude;
66 	intensity = (unsigned int)(volt_mag_multi >> MAX_MAGNITUDE_SHIFT);
67 
68 	error = regulator_set_voltage(haptic->regulator,
69 				      intensity + haptic->min_volt,
70 				      haptic->max_volt);
71 	if (error) {
72 		dev_err(haptic->dev, "cannot set regulator voltage to %d: %d\n",
73 			intensity + haptic->min_volt, error);
74 		return error;
75 	}
76 
77 	regulator_haptic_toggle(haptic, !!magnitude);
78 
79 	return 0;
80 }
81 
82 static void regulator_haptic_work(struct work_struct *work)
83 {
84 	struct regulator_haptic *haptic = container_of(work,
85 					struct regulator_haptic, work);
86 
87 	guard(mutex)(&haptic->mutex);
88 
89 	if (!haptic->suspended)
90 		regulator_haptic_set_voltage(haptic, haptic->magnitude);
91 }
92 
93 static int regulator_haptic_play_effect(struct input_dev *input, void *data,
94 					struct ff_effect *effect)
95 {
96 	struct regulator_haptic *haptic = input_get_drvdata(input);
97 
98 	haptic->magnitude = effect->u.rumble.strong_magnitude;
99 	if (!haptic->magnitude)
100 		haptic->magnitude = effect->u.rumble.weak_magnitude;
101 
102 	schedule_work(&haptic->work);
103 
104 	return 0;
105 }
106 
107 static void regulator_haptic_close(struct input_dev *input)
108 {
109 	struct regulator_haptic *haptic = input_get_drvdata(input);
110 
111 	cancel_work_sync(&haptic->work);
112 	regulator_haptic_set_voltage(haptic, 0);
113 }
114 
115 static int __maybe_unused
116 regulator_haptic_parse_dt(struct device *dev, struct regulator_haptic *haptic)
117 {
118 	struct device_node *node;
119 	int error;
120 
121 	node = dev->of_node;
122 	if(!node) {
123 		dev_err(dev, "Missing device tree data\n");
124 		return -EINVAL;
125 	}
126 
127 	error = of_property_read_u32(node, "max-microvolt", &haptic->max_volt);
128 	if (error) {
129 		dev_err(dev, "cannot parse max-microvolt\n");
130 		return error;
131 	}
132 
133 	error = of_property_read_u32(node, "min-microvolt", &haptic->min_volt);
134 	if (error) {
135 		dev_err(dev, "cannot parse min-microvolt\n");
136 		return error;
137 	}
138 
139 	return 0;
140 }
141 
142 static int regulator_haptic_probe(struct platform_device *pdev)
143 {
144 	const struct regulator_haptic_data *pdata = dev_get_platdata(&pdev->dev);
145 	struct regulator_haptic *haptic;
146 	struct input_dev *input_dev;
147 	int error;
148 
149 	haptic = devm_kzalloc_obj(&pdev->dev, *haptic);
150 	if (!haptic)
151 		return -ENOMEM;
152 
153 	platform_set_drvdata(pdev, haptic);
154 	haptic->dev = &pdev->dev;
155 	mutex_init(&haptic->mutex);
156 	INIT_WORK(&haptic->work, regulator_haptic_work);
157 
158 	if (pdata) {
159 		haptic->max_volt = pdata->max_volt;
160 		haptic->min_volt = pdata->min_volt;
161 	} else if (IS_ENABLED(CONFIG_OF)) {
162 		error = regulator_haptic_parse_dt(&pdev->dev, haptic);
163 		if (error)
164 			return error;
165 	} else {
166 		dev_err(&pdev->dev, "Missing platform data\n");
167 		return -EINVAL;
168 	}
169 
170 	haptic->regulator = devm_regulator_get_exclusive(&pdev->dev, "haptic");
171 	if (IS_ERR(haptic->regulator)) {
172 		dev_err(&pdev->dev, "failed to get regulator\n");
173 		return PTR_ERR(haptic->regulator);
174 	}
175 
176 	input_dev = devm_input_allocate_device(&pdev->dev);
177 	if (!input_dev)
178 		return	-ENOMEM;
179 
180 	haptic->input_dev = input_dev;
181 	haptic->input_dev->name = "regulator-haptic";
182 	haptic->input_dev->close = regulator_haptic_close;
183 	input_set_drvdata(haptic->input_dev, haptic);
184 	input_set_capability(haptic->input_dev, EV_FF, FF_RUMBLE);
185 
186 	error = input_ff_create_memless(input_dev, NULL,
187 					regulator_haptic_play_effect);
188 	if (error) {
189 		dev_err(&pdev->dev, "failed to create force-feedback\n");
190 		return error;
191 	}
192 
193 	error = input_register_device(haptic->input_dev);
194 	if (error) {
195 		dev_err(&pdev->dev, "failed to register input device\n");
196 		return error;
197 	}
198 
199 	return 0;
200 }
201 
202 static int regulator_haptic_suspend(struct device *dev)
203 {
204 	struct platform_device *pdev = to_platform_device(dev);
205 	struct regulator_haptic *haptic = platform_get_drvdata(pdev);
206 
207 	scoped_guard(mutex_intr, &haptic->mutex) {
208 		regulator_haptic_set_voltage(haptic, 0);
209 		haptic->suspended = true;
210 
211 		return 0;
212 	}
213 
214 	return -EINTR;
215 }
216 
217 static int regulator_haptic_resume(struct device *dev)
218 {
219 	struct platform_device *pdev = to_platform_device(dev);
220 	struct regulator_haptic *haptic = platform_get_drvdata(pdev);
221 	unsigned int magnitude;
222 
223 	guard(mutex)(&haptic->mutex);
224 
225 	haptic->suspended = false;
226 
227 	magnitude = READ_ONCE(haptic->magnitude);
228 	if (magnitude)
229 		regulator_haptic_set_voltage(haptic, magnitude);
230 
231 	return 0;
232 }
233 
234 static DEFINE_SIMPLE_DEV_PM_OPS(regulator_haptic_pm_ops,
235 		regulator_haptic_suspend, regulator_haptic_resume);
236 
237 static const struct of_device_id regulator_haptic_dt_match[] = {
238 	{ .compatible = "regulator-haptic" },
239 	{ /* sentinel */ },
240 };
241 MODULE_DEVICE_TABLE(of, regulator_haptic_dt_match);
242 
243 static struct platform_driver regulator_haptic_driver = {
244 	.probe		= regulator_haptic_probe,
245 	.driver		= {
246 		.name		= "regulator-haptic",
247 		.of_match_table = regulator_haptic_dt_match,
248 		.pm		= pm_sleep_ptr(&regulator_haptic_pm_ops),
249 	},
250 };
251 module_platform_driver(regulator_haptic_driver);
252 
253 MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
254 MODULE_AUTHOR("Hyunhee Kim <hyunhee.kim@samsung.com>");
255 MODULE_DESCRIPTION("Regulator haptic driver");
256 MODULE_LICENSE("GPL");
257