xref: /linux/drivers/dpll/dpll_core.c (revision ea518afc992032f7570c0a89ac9240b387dc0faf)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  dpll_core.c - DPLL subsystem kernel-space interface implementation.
4  *
5  *  Copyright (c) 2023 Meta Platforms, Inc. and affiliates
6  *  Copyright (c) 2023 Intel Corporation.
7  */
8 
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
15 
16 #include "dpll_core.h"
17 #include "dpll_netlink.h"
18 
19 /* Mutex lock to protect DPLL subsystem devices and pins */
20 DEFINE_MUTEX(dpll_lock);
21 
22 DEFINE_XARRAY_FLAGS(dpll_device_xa, XA_FLAGS_ALLOC);
23 DEFINE_XARRAY_FLAGS(dpll_pin_xa, XA_FLAGS_ALLOC);
24 
25 static u32 dpll_device_xa_id;
26 static u32 dpll_pin_xa_id;
27 
28 #define ASSERT_DPLL_REGISTERED(d)	\
29 	WARN_ON_ONCE(!xa_get_mark(&dpll_device_xa, (d)->id, DPLL_REGISTERED))
30 #define ASSERT_DPLL_NOT_REGISTERED(d)	\
31 	WARN_ON_ONCE(xa_get_mark(&dpll_device_xa, (d)->id, DPLL_REGISTERED))
32 
33 struct dpll_device_registration {
34 	struct list_head list;
35 	const struct dpll_device_ops *ops;
36 	void *priv;
37 };
38 
39 struct dpll_pin_registration {
40 	struct list_head list;
41 	const struct dpll_pin_ops *ops;
42 	void *priv;
43 };
44 
45 struct dpll_device *dpll_device_get_by_id(int id)
46 {
47 	if (xa_get_mark(&dpll_device_xa, id, DPLL_REGISTERED))
48 		return xa_load(&dpll_device_xa, id);
49 
50 	return NULL;
51 }
52 
53 static struct dpll_pin_registration *
54 dpll_pin_registration_find(struct dpll_pin_ref *ref,
55 			   const struct dpll_pin_ops *ops, void *priv)
56 {
57 	struct dpll_pin_registration *reg;
58 
59 	list_for_each_entry(reg, &ref->registration_list, list) {
60 		if (reg->ops == ops && reg->priv == priv)
61 			return reg;
62 	}
63 	return NULL;
64 }
65 
66 static int
67 dpll_xa_ref_pin_add(struct xarray *xa_pins, struct dpll_pin *pin,
68 		    const struct dpll_pin_ops *ops, void *priv)
69 {
70 	struct dpll_pin_registration *reg;
71 	struct dpll_pin_ref *ref;
72 	bool ref_exists = false;
73 	unsigned long i;
74 	int ret;
75 
76 	xa_for_each(xa_pins, i, ref) {
77 		if (ref->pin != pin)
78 			continue;
79 		reg = dpll_pin_registration_find(ref, ops, priv);
80 		if (reg) {
81 			refcount_inc(&ref->refcount);
82 			return 0;
83 		}
84 		ref_exists = true;
85 		break;
86 	}
87 
88 	if (!ref_exists) {
89 		ref = kzalloc(sizeof(*ref), GFP_KERNEL);
90 		if (!ref)
91 			return -ENOMEM;
92 		ref->pin = pin;
93 		INIT_LIST_HEAD(&ref->registration_list);
94 		ret = xa_insert(xa_pins, pin->pin_idx, ref, GFP_KERNEL);
95 		if (ret) {
96 			kfree(ref);
97 			return ret;
98 		}
99 		refcount_set(&ref->refcount, 1);
100 	}
101 
102 	reg = kzalloc(sizeof(*reg), GFP_KERNEL);
103 	if (!reg) {
104 		if (!ref_exists) {
105 			xa_erase(xa_pins, pin->pin_idx);
106 			kfree(ref);
107 		}
108 		return -ENOMEM;
109 	}
110 	reg->ops = ops;
111 	reg->priv = priv;
112 	if (ref_exists)
113 		refcount_inc(&ref->refcount);
114 	list_add_tail(&reg->list, &ref->registration_list);
115 
116 	return 0;
117 }
118 
119 static int dpll_xa_ref_pin_del(struct xarray *xa_pins, struct dpll_pin *pin,
120 			       const struct dpll_pin_ops *ops, void *priv)
121 {
122 	struct dpll_pin_registration *reg;
123 	struct dpll_pin_ref *ref;
124 	unsigned long i;
125 
126 	xa_for_each(xa_pins, i, ref) {
127 		if (ref->pin != pin)
128 			continue;
129 		reg = dpll_pin_registration_find(ref, ops, priv);
130 		if (WARN_ON(!reg))
131 			return -EINVAL;
132 		if (refcount_dec_and_test(&ref->refcount)) {
133 			list_del(&reg->list);
134 			kfree(reg);
135 			xa_erase(xa_pins, i);
136 			WARN_ON(!list_empty(&ref->registration_list));
137 			kfree(ref);
138 		}
139 		return 0;
140 	}
141 
142 	return -EINVAL;
143 }
144 
145 static int
146 dpll_xa_ref_dpll_add(struct xarray *xa_dplls, struct dpll_device *dpll,
147 		     const struct dpll_pin_ops *ops, void *priv)
148 {
149 	struct dpll_pin_registration *reg;
150 	struct dpll_pin_ref *ref;
151 	bool ref_exists = false;
152 	unsigned long i;
153 	int ret;
154 
155 	xa_for_each(xa_dplls, i, ref) {
156 		if (ref->dpll != dpll)
157 			continue;
158 		reg = dpll_pin_registration_find(ref, ops, priv);
159 		if (reg) {
160 			refcount_inc(&ref->refcount);
161 			return 0;
162 		}
163 		ref_exists = true;
164 		break;
165 	}
166 
167 	if (!ref_exists) {
168 		ref = kzalloc(sizeof(*ref), GFP_KERNEL);
169 		if (!ref)
170 			return -ENOMEM;
171 		ref->dpll = dpll;
172 		INIT_LIST_HEAD(&ref->registration_list);
173 		ret = xa_insert(xa_dplls, dpll->id, ref, GFP_KERNEL);
174 		if (ret) {
175 			kfree(ref);
176 			return ret;
177 		}
178 		refcount_set(&ref->refcount, 1);
179 	}
180 
181 	reg = kzalloc(sizeof(*reg), GFP_KERNEL);
182 	if (!reg) {
183 		if (!ref_exists) {
184 			xa_erase(xa_dplls, dpll->id);
185 			kfree(ref);
186 		}
187 		return -ENOMEM;
188 	}
189 	reg->ops = ops;
190 	reg->priv = priv;
191 	if (ref_exists)
192 		refcount_inc(&ref->refcount);
193 	list_add_tail(&reg->list, &ref->registration_list);
194 
195 	return 0;
196 }
197 
198 static void
199 dpll_xa_ref_dpll_del(struct xarray *xa_dplls, struct dpll_device *dpll,
200 		     const struct dpll_pin_ops *ops, void *priv)
201 {
202 	struct dpll_pin_registration *reg;
203 	struct dpll_pin_ref *ref;
204 	unsigned long i;
205 
206 	xa_for_each(xa_dplls, i, ref) {
207 		if (ref->dpll != dpll)
208 			continue;
209 		reg = dpll_pin_registration_find(ref, ops, priv);
210 		if (WARN_ON(!reg))
211 			return;
212 		if (refcount_dec_and_test(&ref->refcount)) {
213 			list_del(&reg->list);
214 			kfree(reg);
215 			xa_erase(xa_dplls, i);
216 			WARN_ON(!list_empty(&ref->registration_list));
217 			kfree(ref);
218 		}
219 		return;
220 	}
221 }
222 
223 struct dpll_pin_ref *dpll_xa_ref_dpll_first(struct xarray *xa_refs)
224 {
225 	struct dpll_pin_ref *ref;
226 	unsigned long i = 0;
227 
228 	ref = xa_find(xa_refs, &i, ULONG_MAX, XA_PRESENT);
229 	WARN_ON(!ref);
230 	return ref;
231 }
232 
233 static struct dpll_device *
234 dpll_device_alloc(const u64 clock_id, u32 device_idx, struct module *module)
235 {
236 	struct dpll_device *dpll;
237 	int ret;
238 
239 	dpll = kzalloc(sizeof(*dpll), GFP_KERNEL);
240 	if (!dpll)
241 		return ERR_PTR(-ENOMEM);
242 	refcount_set(&dpll->refcount, 1);
243 	INIT_LIST_HEAD(&dpll->registration_list);
244 	dpll->device_idx = device_idx;
245 	dpll->clock_id = clock_id;
246 	dpll->module = module;
247 	ret = xa_alloc_cyclic(&dpll_device_xa, &dpll->id, dpll, xa_limit_32b,
248 			      &dpll_device_xa_id, GFP_KERNEL);
249 	if (ret < 0) {
250 		kfree(dpll);
251 		return ERR_PTR(ret);
252 	}
253 	xa_init_flags(&dpll->pin_refs, XA_FLAGS_ALLOC);
254 
255 	return dpll;
256 }
257 
258 /**
259  * dpll_device_get - find existing or create new dpll device
260  * @clock_id: clock_id of creator
261  * @device_idx: idx given by device driver
262  * @module: reference to registering module
263  *
264  * Get existing object of a dpll device, unique for given arguments.
265  * Create new if doesn't exist yet.
266  *
267  * Context: Acquires a lock (dpll_lock)
268  * Return:
269  * * valid dpll_device struct pointer if succeeded
270  * * ERR_PTR(X) - error
271  */
272 struct dpll_device *
273 dpll_device_get(u64 clock_id, u32 device_idx, struct module *module)
274 {
275 	struct dpll_device *dpll, *ret = NULL;
276 	unsigned long index;
277 
278 	mutex_lock(&dpll_lock);
279 	xa_for_each(&dpll_device_xa, index, dpll) {
280 		if (dpll->clock_id == clock_id &&
281 		    dpll->device_idx == device_idx &&
282 		    dpll->module == module) {
283 			ret = dpll;
284 			refcount_inc(&ret->refcount);
285 			break;
286 		}
287 	}
288 	if (!ret)
289 		ret = dpll_device_alloc(clock_id, device_idx, module);
290 	mutex_unlock(&dpll_lock);
291 
292 	return ret;
293 }
294 EXPORT_SYMBOL_GPL(dpll_device_get);
295 
296 /**
297  * dpll_device_put - decrease the refcount and free memory if possible
298  * @dpll: dpll_device struct pointer
299  *
300  * Context: Acquires a lock (dpll_lock)
301  * Drop reference for a dpll device, if all references are gone, delete
302  * dpll device object.
303  */
304 void dpll_device_put(struct dpll_device *dpll)
305 {
306 	mutex_lock(&dpll_lock);
307 	if (refcount_dec_and_test(&dpll->refcount)) {
308 		ASSERT_DPLL_NOT_REGISTERED(dpll);
309 		WARN_ON_ONCE(!xa_empty(&dpll->pin_refs));
310 		xa_destroy(&dpll->pin_refs);
311 		xa_erase(&dpll_device_xa, dpll->id);
312 		WARN_ON(!list_empty(&dpll->registration_list));
313 		kfree(dpll);
314 	}
315 	mutex_unlock(&dpll_lock);
316 }
317 EXPORT_SYMBOL_GPL(dpll_device_put);
318 
319 static struct dpll_device_registration *
320 dpll_device_registration_find(struct dpll_device *dpll,
321 			      const struct dpll_device_ops *ops, void *priv)
322 {
323 	struct dpll_device_registration *reg;
324 
325 	list_for_each_entry(reg, &dpll->registration_list, list) {
326 		if (reg->ops == ops && reg->priv == priv)
327 			return reg;
328 	}
329 	return NULL;
330 }
331 
332 /**
333  * dpll_device_register - register the dpll device in the subsystem
334  * @dpll: pointer to a dpll
335  * @type: type of a dpll
336  * @ops: ops for a dpll device
337  * @priv: pointer to private information of owner
338  *
339  * Make dpll device available for user space.
340  *
341  * Context: Acquires a lock (dpll_lock)
342  * Return:
343  * * 0 on success
344  * * negative - error value
345  */
346 int dpll_device_register(struct dpll_device *dpll, enum dpll_type type,
347 			 const struct dpll_device_ops *ops, void *priv)
348 {
349 	struct dpll_device_registration *reg;
350 	bool first_registration = false;
351 
352 	if (WARN_ON(!ops))
353 		return -EINVAL;
354 	if (WARN_ON(!ops->mode_get))
355 		return -EINVAL;
356 	if (WARN_ON(!ops->lock_status_get))
357 		return -EINVAL;
358 	if (WARN_ON(type < DPLL_TYPE_PPS || type > DPLL_TYPE_MAX))
359 		return -EINVAL;
360 
361 	mutex_lock(&dpll_lock);
362 	reg = dpll_device_registration_find(dpll, ops, priv);
363 	if (reg) {
364 		mutex_unlock(&dpll_lock);
365 		return -EEXIST;
366 	}
367 
368 	reg = kzalloc(sizeof(*reg), GFP_KERNEL);
369 	if (!reg) {
370 		mutex_unlock(&dpll_lock);
371 		return -ENOMEM;
372 	}
373 	reg->ops = ops;
374 	reg->priv = priv;
375 	dpll->type = type;
376 	first_registration = list_empty(&dpll->registration_list);
377 	list_add_tail(&reg->list, &dpll->registration_list);
378 	if (!first_registration) {
379 		mutex_unlock(&dpll_lock);
380 		return 0;
381 	}
382 
383 	xa_set_mark(&dpll_device_xa, dpll->id, DPLL_REGISTERED);
384 	dpll_device_create_ntf(dpll);
385 	mutex_unlock(&dpll_lock);
386 
387 	return 0;
388 }
389 EXPORT_SYMBOL_GPL(dpll_device_register);
390 
391 /**
392  * dpll_device_unregister - unregister dpll device
393  * @dpll: registered dpll pointer
394  * @ops: ops for a dpll device
395  * @priv: pointer to private information of owner
396  *
397  * Unregister device, make it unavailable for userspace.
398  * Note: It does not free the memory
399  * Context: Acquires a lock (dpll_lock)
400  */
401 void dpll_device_unregister(struct dpll_device *dpll,
402 			    const struct dpll_device_ops *ops, void *priv)
403 {
404 	struct dpll_device_registration *reg;
405 
406 	mutex_lock(&dpll_lock);
407 	ASSERT_DPLL_REGISTERED(dpll);
408 	dpll_device_delete_ntf(dpll);
409 	reg = dpll_device_registration_find(dpll, ops, priv);
410 	if (WARN_ON(!reg)) {
411 		mutex_unlock(&dpll_lock);
412 		return;
413 	}
414 	list_del(&reg->list);
415 	kfree(reg);
416 
417 	if (!list_empty(&dpll->registration_list)) {
418 		mutex_unlock(&dpll_lock);
419 		return;
420 	}
421 	xa_clear_mark(&dpll_device_xa, dpll->id, DPLL_REGISTERED);
422 	mutex_unlock(&dpll_lock);
423 }
424 EXPORT_SYMBOL_GPL(dpll_device_unregister);
425 
426 static void dpll_pin_prop_free(struct dpll_pin_properties *prop)
427 {
428 	kfree(prop->package_label);
429 	kfree(prop->panel_label);
430 	kfree(prop->board_label);
431 	kfree(prop->freq_supported);
432 }
433 
434 static int dpll_pin_prop_dup(const struct dpll_pin_properties *src,
435 			     struct dpll_pin_properties *dst)
436 {
437 	memcpy(dst, src, sizeof(*dst));
438 	if (src->freq_supported && src->freq_supported_num) {
439 		size_t freq_size = src->freq_supported_num *
440 				   sizeof(*src->freq_supported);
441 		dst->freq_supported = kmemdup(src->freq_supported,
442 					      freq_size, GFP_KERNEL);
443 		if (!src->freq_supported)
444 			return -ENOMEM;
445 	}
446 	if (src->board_label) {
447 		dst->board_label = kstrdup(src->board_label, GFP_KERNEL);
448 		if (!dst->board_label)
449 			goto err_board_label;
450 	}
451 	if (src->panel_label) {
452 		dst->panel_label = kstrdup(src->panel_label, GFP_KERNEL);
453 		if (!dst->panel_label)
454 			goto err_panel_label;
455 	}
456 	if (src->package_label) {
457 		dst->package_label = kstrdup(src->package_label, GFP_KERNEL);
458 		if (!dst->package_label)
459 			goto err_package_label;
460 	}
461 
462 	return 0;
463 
464 err_package_label:
465 	kfree(dst->panel_label);
466 err_panel_label:
467 	kfree(dst->board_label);
468 err_board_label:
469 	kfree(dst->freq_supported);
470 	return -ENOMEM;
471 }
472 
473 static struct dpll_pin *
474 dpll_pin_alloc(u64 clock_id, u32 pin_idx, struct module *module,
475 	       const struct dpll_pin_properties *prop)
476 {
477 	struct dpll_pin *pin;
478 	int ret;
479 
480 	pin = kzalloc(sizeof(*pin), GFP_KERNEL);
481 	if (!pin)
482 		return ERR_PTR(-ENOMEM);
483 	pin->pin_idx = pin_idx;
484 	pin->clock_id = clock_id;
485 	pin->module = module;
486 	if (WARN_ON(prop->type < DPLL_PIN_TYPE_MUX ||
487 		    prop->type > DPLL_PIN_TYPE_MAX)) {
488 		ret = -EINVAL;
489 		goto err_pin_prop;
490 	}
491 	ret = dpll_pin_prop_dup(prop, &pin->prop);
492 	if (ret)
493 		goto err_pin_prop;
494 	refcount_set(&pin->refcount, 1);
495 	xa_init_flags(&pin->dpll_refs, XA_FLAGS_ALLOC);
496 	xa_init_flags(&pin->parent_refs, XA_FLAGS_ALLOC);
497 	ret = xa_alloc_cyclic(&dpll_pin_xa, &pin->id, pin, xa_limit_32b,
498 			      &dpll_pin_xa_id, GFP_KERNEL);
499 	if (ret)
500 		goto err_xa_alloc;
501 	return pin;
502 err_xa_alloc:
503 	xa_destroy(&pin->dpll_refs);
504 	xa_destroy(&pin->parent_refs);
505 	dpll_pin_prop_free(&pin->prop);
506 err_pin_prop:
507 	kfree(pin);
508 	return ERR_PTR(ret);
509 }
510 
511 /**
512  * dpll_pin_get - find existing or create new dpll pin
513  * @clock_id: clock_id of creator
514  * @pin_idx: idx given by dev driver
515  * @module: reference to registering module
516  * @prop: dpll pin properties
517  *
518  * Get existing object of a pin (unique for given arguments) or create new
519  * if doesn't exist yet.
520  *
521  * Context: Acquires a lock (dpll_lock)
522  * Return:
523  * * valid allocated dpll_pin struct pointer if succeeded
524  * * ERR_PTR(X) - error
525  */
526 struct dpll_pin *
527 dpll_pin_get(u64 clock_id, u32 pin_idx, struct module *module,
528 	     const struct dpll_pin_properties *prop)
529 {
530 	struct dpll_pin *pos, *ret = NULL;
531 	unsigned long i;
532 
533 	mutex_lock(&dpll_lock);
534 	xa_for_each(&dpll_pin_xa, i, pos) {
535 		if (pos->clock_id == clock_id &&
536 		    pos->pin_idx == pin_idx &&
537 		    pos->module == module) {
538 			ret = pos;
539 			refcount_inc(&ret->refcount);
540 			break;
541 		}
542 	}
543 	if (!ret)
544 		ret = dpll_pin_alloc(clock_id, pin_idx, module, prop);
545 	mutex_unlock(&dpll_lock);
546 
547 	return ret;
548 }
549 EXPORT_SYMBOL_GPL(dpll_pin_get);
550 
551 /**
552  * dpll_pin_put - decrease the refcount and free memory if possible
553  * @pin: pointer to a pin to be put
554  *
555  * Drop reference for a pin, if all references are gone, delete pin object.
556  *
557  * Context: Acquires a lock (dpll_lock)
558  */
559 void dpll_pin_put(struct dpll_pin *pin)
560 {
561 	mutex_lock(&dpll_lock);
562 	if (refcount_dec_and_test(&pin->refcount)) {
563 		xa_destroy(&pin->dpll_refs);
564 		xa_destroy(&pin->parent_refs);
565 		xa_erase(&dpll_pin_xa, pin->id);
566 		dpll_pin_prop_free(&pin->prop);
567 		kfree(pin);
568 	}
569 	mutex_unlock(&dpll_lock);
570 }
571 EXPORT_SYMBOL_GPL(dpll_pin_put);
572 
573 static int
574 __dpll_pin_register(struct dpll_device *dpll, struct dpll_pin *pin,
575 		    const struct dpll_pin_ops *ops, void *priv)
576 {
577 	int ret;
578 
579 	ret = dpll_xa_ref_pin_add(&dpll->pin_refs, pin, ops, priv);
580 	if (ret)
581 		return ret;
582 	ret = dpll_xa_ref_dpll_add(&pin->dpll_refs, dpll, ops, priv);
583 	if (ret)
584 		goto ref_pin_del;
585 	xa_set_mark(&dpll_pin_xa, pin->id, DPLL_REGISTERED);
586 	dpll_pin_create_ntf(pin);
587 
588 	return ret;
589 
590 ref_pin_del:
591 	dpll_xa_ref_pin_del(&dpll->pin_refs, pin, ops, priv);
592 	return ret;
593 }
594 
595 /**
596  * dpll_pin_register - register the dpll pin in the subsystem
597  * @dpll: pointer to a dpll
598  * @pin: pointer to a dpll pin
599  * @ops: ops for a dpll pin ops
600  * @priv: pointer to private information of owner
601  *
602  * Context: Acquires a lock (dpll_lock)
603  * Return:
604  * * 0 on success
605  * * negative - error value
606  */
607 int
608 dpll_pin_register(struct dpll_device *dpll, struct dpll_pin *pin,
609 		  const struct dpll_pin_ops *ops, void *priv)
610 {
611 	int ret;
612 
613 	if (WARN_ON(!ops) ||
614 	    WARN_ON(!ops->state_on_dpll_get) ||
615 	    WARN_ON(!ops->direction_get))
616 		return -EINVAL;
617 
618 	mutex_lock(&dpll_lock);
619 	if (WARN_ON(!(dpll->module == pin->module &&
620 		      dpll->clock_id == pin->clock_id)))
621 		ret = -EINVAL;
622 	else
623 		ret = __dpll_pin_register(dpll, pin, ops, priv);
624 	mutex_unlock(&dpll_lock);
625 
626 	return ret;
627 }
628 EXPORT_SYMBOL_GPL(dpll_pin_register);
629 
630 static void
631 __dpll_pin_unregister(struct dpll_device *dpll, struct dpll_pin *pin,
632 		      const struct dpll_pin_ops *ops, void *priv)
633 {
634 	dpll_xa_ref_pin_del(&dpll->pin_refs, pin, ops, priv);
635 	dpll_xa_ref_dpll_del(&pin->dpll_refs, dpll, ops, priv);
636 	if (xa_empty(&pin->dpll_refs))
637 		xa_clear_mark(&dpll_pin_xa, pin->id, DPLL_REGISTERED);
638 }
639 
640 /**
641  * dpll_pin_unregister - unregister dpll pin from dpll device
642  * @dpll: registered dpll pointer
643  * @pin: pointer to a pin
644  * @ops: ops for a dpll pin
645  * @priv: pointer to private information of owner
646  *
647  * Note: It does not free the memory
648  * Context: Acquires a lock (dpll_lock)
649  */
650 void dpll_pin_unregister(struct dpll_device *dpll, struct dpll_pin *pin,
651 			 const struct dpll_pin_ops *ops, void *priv)
652 {
653 	if (WARN_ON(xa_empty(&dpll->pin_refs)))
654 		return;
655 	if (WARN_ON(!xa_empty(&pin->parent_refs)))
656 		return;
657 
658 	mutex_lock(&dpll_lock);
659 	dpll_pin_delete_ntf(pin);
660 	__dpll_pin_unregister(dpll, pin, ops, priv);
661 	mutex_unlock(&dpll_lock);
662 }
663 EXPORT_SYMBOL_GPL(dpll_pin_unregister);
664 
665 /**
666  * dpll_pin_on_pin_register - register a pin with a parent pin
667  * @parent: pointer to a parent pin
668  * @pin: pointer to a pin
669  * @ops: ops for a dpll pin
670  * @priv: pointer to private information of owner
671  *
672  * Register a pin with a parent pin, create references between them and
673  * between newly registered pin and dplls connected with a parent pin.
674  *
675  * Context: Acquires a lock (dpll_lock)
676  * Return:
677  * * 0 on success
678  * * negative - error value
679  */
680 int dpll_pin_on_pin_register(struct dpll_pin *parent, struct dpll_pin *pin,
681 			     const struct dpll_pin_ops *ops, void *priv)
682 {
683 	struct dpll_pin_ref *ref;
684 	unsigned long i, stop;
685 	int ret;
686 
687 	if (WARN_ON(parent->prop.type != DPLL_PIN_TYPE_MUX))
688 		return -EINVAL;
689 
690 	if (WARN_ON(!ops) ||
691 	    WARN_ON(!ops->state_on_pin_get) ||
692 	    WARN_ON(!ops->direction_get))
693 		return -EINVAL;
694 
695 	mutex_lock(&dpll_lock);
696 	ret = dpll_xa_ref_pin_add(&pin->parent_refs, parent, ops, priv);
697 	if (ret)
698 		goto unlock;
699 	refcount_inc(&pin->refcount);
700 	xa_for_each(&parent->dpll_refs, i, ref) {
701 		ret = __dpll_pin_register(ref->dpll, pin, ops, priv);
702 		if (ret) {
703 			stop = i;
704 			goto dpll_unregister;
705 		}
706 		dpll_pin_create_ntf(pin);
707 	}
708 	mutex_unlock(&dpll_lock);
709 
710 	return ret;
711 
712 dpll_unregister:
713 	xa_for_each(&parent->dpll_refs, i, ref)
714 		if (i < stop) {
715 			__dpll_pin_unregister(ref->dpll, pin, ops, priv);
716 			dpll_pin_delete_ntf(pin);
717 		}
718 	refcount_dec(&pin->refcount);
719 	dpll_xa_ref_pin_del(&pin->parent_refs, parent, ops, priv);
720 unlock:
721 	mutex_unlock(&dpll_lock);
722 	return ret;
723 }
724 EXPORT_SYMBOL_GPL(dpll_pin_on_pin_register);
725 
726 /**
727  * dpll_pin_on_pin_unregister - unregister dpll pin from a parent pin
728  * @parent: pointer to a parent pin
729  * @pin: pointer to a pin
730  * @ops: ops for a dpll pin
731  * @priv: pointer to private information of owner
732  *
733  * Context: Acquires a lock (dpll_lock)
734  * Note: It does not free the memory
735  */
736 void dpll_pin_on_pin_unregister(struct dpll_pin *parent, struct dpll_pin *pin,
737 				const struct dpll_pin_ops *ops, void *priv)
738 {
739 	struct dpll_pin_ref *ref;
740 	unsigned long i;
741 
742 	mutex_lock(&dpll_lock);
743 	dpll_pin_delete_ntf(pin);
744 	dpll_xa_ref_pin_del(&pin->parent_refs, parent, ops, priv);
745 	refcount_dec(&pin->refcount);
746 	xa_for_each(&pin->dpll_refs, i, ref)
747 		__dpll_pin_unregister(ref->dpll, pin, ops, priv);
748 	mutex_unlock(&dpll_lock);
749 }
750 EXPORT_SYMBOL_GPL(dpll_pin_on_pin_unregister);
751 
752 static struct dpll_device_registration *
753 dpll_device_registration_first(struct dpll_device *dpll)
754 {
755 	struct dpll_device_registration *reg;
756 
757 	reg = list_first_entry_or_null((struct list_head *)&dpll->registration_list,
758 				       struct dpll_device_registration, list);
759 	WARN_ON(!reg);
760 	return reg;
761 }
762 
763 void *dpll_priv(struct dpll_device *dpll)
764 {
765 	struct dpll_device_registration *reg;
766 
767 	reg = dpll_device_registration_first(dpll);
768 	return reg->priv;
769 }
770 
771 const struct dpll_device_ops *dpll_device_ops(struct dpll_device *dpll)
772 {
773 	struct dpll_device_registration *reg;
774 
775 	reg = dpll_device_registration_first(dpll);
776 	return reg->ops;
777 }
778 
779 static struct dpll_pin_registration *
780 dpll_pin_registration_first(struct dpll_pin_ref *ref)
781 {
782 	struct dpll_pin_registration *reg;
783 
784 	reg = list_first_entry_or_null(&ref->registration_list,
785 				       struct dpll_pin_registration, list);
786 	WARN_ON(!reg);
787 	return reg;
788 }
789 
790 void *dpll_pin_on_dpll_priv(struct dpll_device *dpll,
791 			    struct dpll_pin *pin)
792 {
793 	struct dpll_pin_registration *reg;
794 	struct dpll_pin_ref *ref;
795 
796 	ref = xa_load(&dpll->pin_refs, pin->pin_idx);
797 	if (!ref)
798 		return NULL;
799 	reg = dpll_pin_registration_first(ref);
800 	return reg->priv;
801 }
802 
803 void *dpll_pin_on_pin_priv(struct dpll_pin *parent,
804 			   struct dpll_pin *pin)
805 {
806 	struct dpll_pin_registration *reg;
807 	struct dpll_pin_ref *ref;
808 
809 	ref = xa_load(&pin->parent_refs, parent->pin_idx);
810 	if (!ref)
811 		return NULL;
812 	reg = dpll_pin_registration_first(ref);
813 	return reg->priv;
814 }
815 
816 const struct dpll_pin_ops *dpll_pin_ops(struct dpll_pin_ref *ref)
817 {
818 	struct dpll_pin_registration *reg;
819 
820 	reg = dpll_pin_registration_first(ref);
821 	return reg->ops;
822 }
823 
824 static int __init dpll_init(void)
825 {
826 	int ret;
827 
828 	ret = genl_register_family(&dpll_nl_family);
829 	if (ret)
830 		goto error;
831 
832 	return 0;
833 
834 error:
835 	mutex_destroy(&dpll_lock);
836 	return ret;
837 }
838 
839 static void __exit dpll_exit(void)
840 {
841 	genl_unregister_family(&dpll_nl_family);
842 	mutex_destroy(&dpll_lock);
843 }
844 
845 subsys_initcall(dpll_init);
846 module_exit(dpll_exit);
847