xref: /openbmc/linux/drivers/soc/qcom/pmic_glink.c (revision 0e6774ec)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
4  * Copyright (c) 2022, Linaro Ltd
5  */
6 #include <linux/auxiliary_bus.h>
7 #include <linux/module.h>
8 #include <linux/of.h>
9 #include <linux/platform_device.h>
10 #include <linux/rpmsg.h>
11 #include <linux/slab.h>
12 #include <linux/soc/qcom/pdr.h>
13 #include <linux/soc/qcom/pmic_glink.h>
14 #include <linux/spinlock.h>
15 
16 enum {
17 	PMIC_GLINK_CLIENT_BATT = 0,
18 	PMIC_GLINK_CLIENT_ALTMODE,
19 	PMIC_GLINK_CLIENT_UCSI,
20 };
21 
22 #define PMIC_GLINK_CLIENT_DEFAULT	(BIT(PMIC_GLINK_CLIENT_BATT) |	\
23 					 BIT(PMIC_GLINK_CLIENT_ALTMODE))
24 
25 struct pmic_glink {
26 	struct device *dev;
27 	struct pdr_handle *pdr;
28 
29 	struct rpmsg_endpoint *ept;
30 
31 	unsigned long client_mask;
32 
33 	struct auxiliary_device altmode_aux;
34 	struct auxiliary_device ps_aux;
35 	struct auxiliary_device ucsi_aux;
36 
37 	/* serializing client_state and pdr_state updates */
38 	struct mutex state_lock;
39 	unsigned int client_state;
40 	unsigned int pdr_state;
41 
42 	/* serializing clients list updates */
43 	spinlock_t client_lock;
44 	struct list_head clients;
45 };
46 
47 static struct pmic_glink *__pmic_glink;
48 static DEFINE_MUTEX(__pmic_glink_lock);
49 
50 struct pmic_glink_client {
51 	struct list_head node;
52 
53 	struct pmic_glink *pg;
54 	unsigned int id;
55 
56 	void (*cb)(const void *data, size_t len, void *priv);
57 	void (*pdr_notify)(void *priv, int state);
58 	void *priv;
59 };
60 
61 static void _devm_pmic_glink_release_client(struct device *dev, void *res)
62 {
63 	struct pmic_glink_client *client = (struct pmic_glink_client *)res;
64 	struct pmic_glink *pg = client->pg;
65 	unsigned long flags;
66 
67 	spin_lock_irqsave(&pg->client_lock, flags);
68 	list_del(&client->node);
69 	spin_unlock_irqrestore(&pg->client_lock, flags);
70 }
71 
72 struct pmic_glink_client *devm_pmic_glink_client_alloc(struct device *dev,
73 						       unsigned int id,
74 						       void (*cb)(const void *, size_t, void *),
75 						       void (*pdr)(void *, int),
76 						       void *priv)
77 {
78 	struct pmic_glink_client *client;
79 	struct pmic_glink *pg = dev_get_drvdata(dev->parent);
80 
81 	client = devres_alloc(_devm_pmic_glink_release_client, sizeof(*client), GFP_KERNEL);
82 	if (!client)
83 		return ERR_PTR(-ENOMEM);
84 
85 	client->pg = pg;
86 	client->id = id;
87 	client->cb = cb;
88 	client->pdr_notify = pdr;
89 	client->priv = priv;
90 	INIT_LIST_HEAD(&client->node);
91 
92 	devres_add(dev, client);
93 
94 	return client;
95 }
96 EXPORT_SYMBOL_GPL(devm_pmic_glink_client_alloc);
97 
98 void pmic_glink_client_register(struct pmic_glink_client *client)
99 {
100 	struct pmic_glink *pg = client->pg;
101 	unsigned long flags;
102 
103 	mutex_lock(&pg->state_lock);
104 	spin_lock_irqsave(&pg->client_lock, flags);
105 
106 	list_add(&client->node, &pg->clients);
107 	client->pdr_notify(client->priv, pg->client_state);
108 
109 	spin_unlock_irqrestore(&pg->client_lock, flags);
110 	mutex_unlock(&pg->state_lock);
111 
112 }
113 EXPORT_SYMBOL_GPL(pmic_glink_client_register);
114 
115 int pmic_glink_send(struct pmic_glink_client *client, void *data, size_t len)
116 {
117 	struct pmic_glink *pg = client->pg;
118 
119 	return rpmsg_send(pg->ept, data, len);
120 }
121 EXPORT_SYMBOL_GPL(pmic_glink_send);
122 
123 static int pmic_glink_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
124 				     int len, void *priv, u32 addr)
125 {
126 	struct pmic_glink_client *client;
127 	struct pmic_glink_hdr *hdr;
128 	struct pmic_glink *pg = dev_get_drvdata(&rpdev->dev);
129 	unsigned long flags;
130 
131 	if (len < sizeof(*hdr)) {
132 		dev_warn(pg->dev, "ignoring truncated message\n");
133 		return 0;
134 	}
135 
136 	hdr = data;
137 
138 	spin_lock_irqsave(&pg->client_lock, flags);
139 	list_for_each_entry(client, &pg->clients, node) {
140 		if (client->id == le32_to_cpu(hdr->owner))
141 			client->cb(data, len, client->priv);
142 	}
143 	spin_unlock_irqrestore(&pg->client_lock, flags);
144 
145 	return 0;
146 }
147 
148 static void pmic_glink_aux_release(struct device *dev) {}
149 
150 static int pmic_glink_add_aux_device(struct pmic_glink *pg,
151 				     struct auxiliary_device *aux,
152 				     const char *name)
153 {
154 	struct device *parent = pg->dev;
155 	int ret;
156 
157 	aux->name = name;
158 	aux->dev.parent = parent;
159 	aux->dev.release = pmic_glink_aux_release;
160 	device_set_of_node_from_dev(&aux->dev, parent);
161 	ret = auxiliary_device_init(aux);
162 	if (ret)
163 		return ret;
164 
165 	ret = auxiliary_device_add(aux);
166 	if (ret)
167 		auxiliary_device_uninit(aux);
168 
169 	return ret;
170 }
171 
172 static void pmic_glink_del_aux_device(struct pmic_glink *pg,
173 				      struct auxiliary_device *aux)
174 {
175 	auxiliary_device_delete(aux);
176 	auxiliary_device_uninit(aux);
177 }
178 
179 static void pmic_glink_state_notify_clients(struct pmic_glink *pg)
180 {
181 	struct pmic_glink_client *client;
182 	unsigned int new_state = pg->client_state;
183 	unsigned long flags;
184 
185 	if (pg->client_state != SERVREG_SERVICE_STATE_UP) {
186 		if (pg->pdr_state == SERVREG_SERVICE_STATE_UP && pg->ept)
187 			new_state = SERVREG_SERVICE_STATE_UP;
188 	} else {
189 		if (pg->pdr_state == SERVREG_SERVICE_STATE_DOWN || !pg->ept)
190 			new_state = SERVREG_SERVICE_STATE_DOWN;
191 	}
192 
193 	if (new_state != pg->client_state) {
194 		spin_lock_irqsave(&pg->client_lock, flags);
195 		list_for_each_entry(client, &pg->clients, node)
196 			client->pdr_notify(client->priv, new_state);
197 		spin_unlock_irqrestore(&pg->client_lock, flags);
198 		pg->client_state = new_state;
199 	}
200 }
201 
202 static void pmic_glink_pdr_callback(int state, char *svc_path, void *priv)
203 {
204 	struct pmic_glink *pg = priv;
205 
206 	mutex_lock(&pg->state_lock);
207 	pg->pdr_state = state;
208 
209 	pmic_glink_state_notify_clients(pg);
210 	mutex_unlock(&pg->state_lock);
211 }
212 
213 static int pmic_glink_rpmsg_probe(struct rpmsg_device *rpdev)
214 {
215 	struct pmic_glink *pg = __pmic_glink;
216 	int ret = 0;
217 
218 	mutex_lock(&__pmic_glink_lock);
219 	if (!pg) {
220 		ret = dev_err_probe(&rpdev->dev, -ENODEV, "no pmic_glink device to attach to\n");
221 		goto out_unlock;
222 	}
223 
224 	dev_set_drvdata(&rpdev->dev, pg);
225 
226 	mutex_lock(&pg->state_lock);
227 	pg->ept = rpdev->ept;
228 	pmic_glink_state_notify_clients(pg);
229 	mutex_unlock(&pg->state_lock);
230 
231 out_unlock:
232 	mutex_unlock(&__pmic_glink_lock);
233 	return ret;
234 }
235 
236 static void pmic_glink_rpmsg_remove(struct rpmsg_device *rpdev)
237 {
238 	struct pmic_glink *pg;
239 
240 	mutex_lock(&__pmic_glink_lock);
241 	pg = __pmic_glink;
242 	if (!pg)
243 		goto out_unlock;
244 
245 	mutex_lock(&pg->state_lock);
246 	pg->ept = NULL;
247 	pmic_glink_state_notify_clients(pg);
248 	mutex_unlock(&pg->state_lock);
249 out_unlock:
250 	mutex_unlock(&__pmic_glink_lock);
251 }
252 
253 static const struct rpmsg_device_id pmic_glink_rpmsg_id_match[] = {
254 	{ "PMIC_RTR_ADSP_APPS" },
255 	{}
256 };
257 
258 static struct rpmsg_driver pmic_glink_rpmsg_driver = {
259 	.probe = pmic_glink_rpmsg_probe,
260 	.remove = pmic_glink_rpmsg_remove,
261 	.callback = pmic_glink_rpmsg_callback,
262 	.id_table = pmic_glink_rpmsg_id_match,
263 	.drv  = {
264 		.name  = "qcom_pmic_glink_rpmsg",
265 	},
266 };
267 
268 static int pmic_glink_probe(struct platform_device *pdev)
269 {
270 	const unsigned long *match_data;
271 	struct pdr_service *service;
272 	struct pmic_glink *pg;
273 	int ret;
274 
275 	pg = devm_kzalloc(&pdev->dev, sizeof(*pg), GFP_KERNEL);
276 	if (!pg)
277 		return -ENOMEM;
278 
279 	dev_set_drvdata(&pdev->dev, pg);
280 
281 	pg->dev = &pdev->dev;
282 
283 	INIT_LIST_HEAD(&pg->clients);
284 	spin_lock_init(&pg->client_lock);
285 	mutex_init(&pg->state_lock);
286 
287 	match_data = (unsigned long *)of_device_get_match_data(&pdev->dev);
288 	if (match_data)
289 		pg->client_mask = *match_data;
290 	else
291 		pg->client_mask = PMIC_GLINK_CLIENT_DEFAULT;
292 
293 	pg->pdr = pdr_handle_alloc(pmic_glink_pdr_callback, pg);
294 	if (IS_ERR(pg->pdr)) {
295 		ret = dev_err_probe(&pdev->dev, PTR_ERR(pg->pdr),
296 				    "failed to initialize pdr\n");
297 		return ret;
298 	}
299 
300 	if (pg->client_mask & BIT(PMIC_GLINK_CLIENT_UCSI)) {
301 		ret = pmic_glink_add_aux_device(pg, &pg->ucsi_aux, "ucsi");
302 		if (ret)
303 			goto out_release_pdr_handle;
304 	}
305 	if (pg->client_mask & BIT(PMIC_GLINK_CLIENT_ALTMODE)) {
306 		ret = pmic_glink_add_aux_device(pg, &pg->altmode_aux, "altmode");
307 		if (ret)
308 			goto out_release_ucsi_aux;
309 	}
310 	if (pg->client_mask & BIT(PMIC_GLINK_CLIENT_BATT)) {
311 		ret = pmic_glink_add_aux_device(pg, &pg->ps_aux, "power-supply");
312 		if (ret)
313 			goto out_release_altmode_aux;
314 	}
315 
316 	service = pdr_add_lookup(pg->pdr, "tms/servreg", "msm/adsp/charger_pd");
317 	if (IS_ERR(service)) {
318 		ret = dev_err_probe(&pdev->dev, PTR_ERR(service),
319 				    "failed adding pdr lookup for charger_pd\n");
320 		goto out_release_aux_devices;
321 	}
322 
323 	mutex_lock(&__pmic_glink_lock);
324 	__pmic_glink = pg;
325 	mutex_unlock(&__pmic_glink_lock);
326 
327 	return 0;
328 
329 out_release_aux_devices:
330 	if (pg->client_mask & BIT(PMIC_GLINK_CLIENT_BATT))
331 		pmic_glink_del_aux_device(pg, &pg->ps_aux);
332 out_release_altmode_aux:
333 	if (pg->client_mask & BIT(PMIC_GLINK_CLIENT_ALTMODE))
334 		pmic_glink_del_aux_device(pg, &pg->altmode_aux);
335 out_release_ucsi_aux:
336 	if (pg->client_mask & BIT(PMIC_GLINK_CLIENT_UCSI))
337 		pmic_glink_del_aux_device(pg, &pg->ucsi_aux);
338 out_release_pdr_handle:
339 	pdr_handle_release(pg->pdr);
340 
341 	return ret;
342 }
343 
344 static int pmic_glink_remove(struct platform_device *pdev)
345 {
346 	struct pmic_glink *pg = dev_get_drvdata(&pdev->dev);
347 
348 	pdr_handle_release(pg->pdr);
349 
350 	if (pg->client_mask & BIT(PMIC_GLINK_CLIENT_BATT))
351 		pmic_glink_del_aux_device(pg, &pg->ps_aux);
352 	if (pg->client_mask & BIT(PMIC_GLINK_CLIENT_ALTMODE))
353 		pmic_glink_del_aux_device(pg, &pg->altmode_aux);
354 	if (pg->client_mask & BIT(PMIC_GLINK_CLIENT_UCSI))
355 		pmic_glink_del_aux_device(pg, &pg->ucsi_aux);
356 
357 	mutex_lock(&__pmic_glink_lock);
358 	__pmic_glink = NULL;
359 	mutex_unlock(&__pmic_glink_lock);
360 
361 	return 0;
362 }
363 
364 static const unsigned long pmic_glink_sm8450_client_mask = BIT(PMIC_GLINK_CLIENT_BATT) |
365 							   BIT(PMIC_GLINK_CLIENT_ALTMODE) |
366 							   BIT(PMIC_GLINK_CLIENT_UCSI);
367 
368 static const struct of_device_id pmic_glink_of_match[] = {
369 	{ .compatible = "qcom,sm8450-pmic-glink", .data = &pmic_glink_sm8450_client_mask },
370 	{ .compatible = "qcom,sm8550-pmic-glink", .data = &pmic_glink_sm8450_client_mask },
371 	{ .compatible = "qcom,pmic-glink" },
372 	{}
373 };
374 MODULE_DEVICE_TABLE(of, pmic_glink_of_match);
375 
376 static struct platform_driver pmic_glink_driver = {
377 	.probe = pmic_glink_probe,
378 	.remove = pmic_glink_remove,
379 	.driver = {
380 		.name = "qcom_pmic_glink",
381 		.of_match_table = pmic_glink_of_match,
382 	},
383 };
384 
385 static int pmic_glink_init(void)
386 {
387 	int ret;
388 
389 	ret = platform_driver_register(&pmic_glink_driver);
390 	if (ret < 0)
391 		return ret;
392 
393 	ret = register_rpmsg_driver(&pmic_glink_rpmsg_driver);
394 	if (ret < 0) {
395 		platform_driver_unregister(&pmic_glink_driver);
396 		return ret;
397 	}
398 
399 	return 0;
400 };
401 module_init(pmic_glink_init);
402 
403 static void pmic_glink_exit(void)
404 {
405 	unregister_rpmsg_driver(&pmic_glink_rpmsg_driver);
406 	platform_driver_unregister(&pmic_glink_driver);
407 };
408 module_exit(pmic_glink_exit);
409 
410 MODULE_DESCRIPTION("Qualcomm PMIC GLINK driver");
411 MODULE_LICENSE("GPL");
412