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