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