xref: /linux/drivers/gpu/drm/nouveau/nvkm/subdev/clk/pllgt215.c (revision e5451c8f8330e03ad3cfa16048b4daf961af434f)
17632b30eSBen Skeggs /*
27632b30eSBen Skeggs  * Copyright 2010 Red Hat Inc.
37632b30eSBen Skeggs  *
47632b30eSBen Skeggs  * Permission is hereby granted, free of charge, to any person obtaining a
57632b30eSBen Skeggs  * copy of this software and associated documentation files (the "Software"),
67632b30eSBen Skeggs  * to deal in the Software without restriction, including without limitation
77632b30eSBen Skeggs  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87632b30eSBen Skeggs  * and/or sell copies of the Software, and to permit persons to whom the
97632b30eSBen Skeggs  * Software is furnished to do so, subject to the following conditions:
107632b30eSBen Skeggs  *
117632b30eSBen Skeggs  * The above copyright notice and this permission notice shall be included in
127632b30eSBen Skeggs  * all copies or substantial portions of the Software.
137632b30eSBen Skeggs  *
147632b30eSBen Skeggs  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
157632b30eSBen Skeggs  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
167632b30eSBen Skeggs  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
177632b30eSBen Skeggs  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
187632b30eSBen Skeggs  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
197632b30eSBen Skeggs  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
207632b30eSBen Skeggs  * OTHER DEALINGS IN THE SOFTWARE.
217632b30eSBen Skeggs  *
227632b30eSBen Skeggs  * Authors: Ben Skeggs
237632b30eSBen Skeggs  */
247632b30eSBen Skeggs #include "pll.h"
257632b30eSBen Skeggs 
267632b30eSBen Skeggs #include <subdev/bios.h>
277632b30eSBen Skeggs #include <subdev/bios/pll.h>
287632b30eSBen Skeggs 
297632b30eSBen Skeggs int
gt215_pll_calc(struct nvkm_subdev * subdev,struct nvbios_pll * info,u32 freq,int * pN,int * pfN,int * pM,int * P)307632b30eSBen Skeggs gt215_pll_calc(struct nvkm_subdev *subdev, struct nvbios_pll *info,
317632b30eSBen Skeggs 	       u32 freq, int *pN, int *pfN, int *pM, int *P)
327632b30eSBen Skeggs {
337632b30eSBen Skeggs 	u32 best_err = ~0, err;
347632b30eSBen Skeggs 	int M, lM, hM, N, fN;
357632b30eSBen Skeggs 
367632b30eSBen Skeggs 	*P = info->vco1.max_freq / freq;
377632b30eSBen Skeggs 	if (*P > info->max_p)
387632b30eSBen Skeggs 		*P = info->max_p;
397632b30eSBen Skeggs 	if (*P < info->min_p)
407632b30eSBen Skeggs 		*P = info->min_p;
417632b30eSBen Skeggs 
427632b30eSBen Skeggs 	lM = (info->refclk + info->vco1.max_inputfreq) / info->vco1.max_inputfreq;
437632b30eSBen Skeggs 	lM = max(lM, (int)info->vco1.min_m);
447632b30eSBen Skeggs 	hM = (info->refclk + info->vco1.min_inputfreq) / info->vco1.min_inputfreq;
457632b30eSBen Skeggs 	hM = min(hM, (int)info->vco1.max_m);
467632b30eSBen Skeggs 	lM = min(lM, hM);
477632b30eSBen Skeggs 
487632b30eSBen Skeggs 	for (M = lM; M <= hM; M++) {
497632b30eSBen Skeggs 		u32 tmp = freq * *P * M;
507632b30eSBen Skeggs 		N  = tmp / info->refclk;
517632b30eSBen Skeggs 		fN = tmp % info->refclk;
527632b30eSBen Skeggs 
537632b30eSBen Skeggs 		if (!pfN) {
547632b30eSBen Skeggs 			if (fN >= info->refclk / 2)
557632b30eSBen Skeggs 				N++;
567632b30eSBen Skeggs 		} else {
577632b30eSBen Skeggs 			if (fN <  info->refclk / 2)
587632b30eSBen Skeggs 				N--;
597632b30eSBen Skeggs 			fN = tmp - (N * info->refclk);
607632b30eSBen Skeggs 		}
617632b30eSBen Skeggs 
627632b30eSBen Skeggs 		if (N < info->vco1.min_n)
637632b30eSBen Skeggs 			continue;
647632b30eSBen Skeggs 		if (N > info->vco1.max_n)
657632b30eSBen Skeggs 			break;
667632b30eSBen Skeggs 
677632b30eSBen Skeggs 		err = abs(freq - (info->refclk * N / M / *P));
687632b30eSBen Skeggs 		if (err < best_err) {
697632b30eSBen Skeggs 			best_err = err;
707632b30eSBen Skeggs 			*pN = N;
717632b30eSBen Skeggs 			*pM = M;
727632b30eSBen Skeggs 		}
737632b30eSBen Skeggs 
747632b30eSBen Skeggs 		if (pfN) {
757632b30eSBen Skeggs 			*pfN = ((fN << 13) + info->refclk / 2) / info->refclk;
767632b30eSBen Skeggs 			*pfN = (*pfN - 4096) & 0xffff;
777632b30eSBen Skeggs 			return freq;
787632b30eSBen Skeggs 		}
797632b30eSBen Skeggs 	}
807632b30eSBen Skeggs 
817632b30eSBen Skeggs 	if (unlikely(best_err == ~0)) {
82*b907649eSBen Skeggs 		nvkm_error(subdev, "unable to find matching pll values\n");
837632b30eSBen Skeggs 		return -EINVAL;
847632b30eSBen Skeggs 	}
857632b30eSBen Skeggs 
867632b30eSBen Skeggs 	return info->refclk * *pN / *pM / *P;
877632b30eSBen Skeggs }
88