18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
48c2ecf20Sopenharmony_ci * Author: Jyri Sarha <jsarha@ti.com>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/device.h>
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include "tidss_scale_coefs.h"
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/*
138c2ecf20Sopenharmony_ci * These are interpolated with a custom python script from DSS5
148c2ecf20Sopenharmony_ci * (drivers/gpu/drm/omapdrm/dss/dispc_coef.c) coefficients.
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef5_m32 = {
178c2ecf20Sopenharmony_ci	.c2 = { 28, 34, 40, 46, 52, 58, 64, 70, 0, 2, 4, 8, 12, 16, 20, 24, },
188c2ecf20Sopenharmony_ci	.c1 = { 132, 138, 144, 150, 156, 162, 168, 174, 76, 84, 92, 98, 104, 110, 116, 124, },
198c2ecf20Sopenharmony_ci	.c0 = { 192, 192, 192, 190, 188, 186, 184, 182, 180, },
208c2ecf20Sopenharmony_ci};
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef5_m26 = {
238c2ecf20Sopenharmony_ci	.c2 = { 24, 28, 32, 38, 44, 50, 56, 64, 0, 2, 4, 6, 8, 12, 16, 20, },
248c2ecf20Sopenharmony_ci	.c1 = { 132, 138, 144, 152, 160, 166, 172, 178, 72, 80, 88, 94, 100, 108, 116, 124, },
258c2ecf20Sopenharmony_ci	.c0 = { 200, 202, 204, 202, 200, 196, 192, 188, 184, },
268c2ecf20Sopenharmony_ci};
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef5_m22 = {
298c2ecf20Sopenharmony_ci	.c2 = { 16, 20, 24, 30, 36, 42, 48, 56, 0, 0, 0, 2, 4, 8, 12, 14, },
308c2ecf20Sopenharmony_ci	.c1 = { 132, 140, 148, 156, 164, 172, 180, 186, 64, 72, 80, 88, 96, 104, 112, 122, },
318c2ecf20Sopenharmony_ci	.c0 = { 216, 216, 216, 214, 212, 208, 204, 198, 192, },
328c2ecf20Sopenharmony_ci};
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef5_m19 = {
358c2ecf20Sopenharmony_ci	.c2 = { 12, 14, 16, 22, 28, 34, 40, 48, 0, 0, 0, 2, 4, 4, 4, 8, },
368c2ecf20Sopenharmony_ci	.c1 = { 128, 140, 152, 160, 168, 176, 184, 192, 56, 64, 72, 82, 92, 100, 108, 118, },
378c2ecf20Sopenharmony_ci	.c0 = { 232, 232, 232, 226, 220, 218, 216, 208, 200, },
388c2ecf20Sopenharmony_ci};
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef5_m16 = {
418c2ecf20Sopenharmony_ci	.c2 = { 0, 2, 4, 8, 12, 18, 24, 32, 0, 0, 0, -2, -4, -4, -4, -2, },
428c2ecf20Sopenharmony_ci	.c1 = { 124, 138, 152, 164, 176, 186, 196, 206, 40, 48, 56, 68, 80, 90, 100, 112, },
438c2ecf20Sopenharmony_ci	.c0 = { 264, 262, 260, 254, 248, 242, 236, 226, 216, },
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef5_m14 = {
478c2ecf20Sopenharmony_ci	.c2 = { -8, -6, -4, -2, 0, 6, 12, 18, 0, -2, -4, -6, -8, -8, -8, -8, },
488c2ecf20Sopenharmony_ci	.c1 = { 120, 134, 148, 164, 180, 194, 208, 220, 24, 32, 40, 52, 64, 78, 92, 106, },
498c2ecf20Sopenharmony_ci	.c0 = { 288, 286, 284, 280, 276, 266, 256, 244, 232, },
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef5_m13 = {
538c2ecf20Sopenharmony_ci	.c2 = { -12, -12, -12, -10, -8, -4, 0, 6, 0, -2, -4, -6, -8, -10, -12, -12, },
548c2ecf20Sopenharmony_ci	.c1 = { 112, 130, 148, 164, 180, 196, 212, 228, 12, 22, 32, 44, 56, 70, 84, 98, },
558c2ecf20Sopenharmony_ci	.c0 = { 312, 308, 304, 298, 292, 282, 272, 258, 244, },
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef5_m12 = {
598c2ecf20Sopenharmony_ci	.c2 = { -16, -18, -20, -18, -16, -14, -12, -6, 0, -2, -4, -6, -8, -10, -12, -14, },
608c2ecf20Sopenharmony_ci	.c1 = { 104, 124, 144, 164, 184, 202, 220, 238, 0, 10, 20, 30, 40, 56, 72, 88, },
618c2ecf20Sopenharmony_ci	.c0 = { 336, 332, 328, 320, 312, 300, 288, 272, 256, },
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef5_m11 = {
658c2ecf20Sopenharmony_ci	.c2 = { -20, -22, -24, -24, -24, -24, -24, -20, 0, -2, -4, -6, -8, -10, -12, -16, },
668c2ecf20Sopenharmony_ci	.c1 = { 92, 114, 136, 158, 180, 204, 228, 250, -16, -8, 0, 12, 24, 38, 52, 72, },
678c2ecf20Sopenharmony_ci	.c0 = { 368, 364, 360, 350, 340, 326, 312, 292, 272, },
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef5_m10 = {
718c2ecf20Sopenharmony_ci	.c2 = { -16, -20, -24, -28, -32, -34, -36, -34, 0, 0, 0, -2, -4, -8, -12, -14, },
728c2ecf20Sopenharmony_ci	.c1 = { 72, 96, 120, 148, 176, 204, 232, 260, -32, -26, -20, -10, 0, 16, 32, 52, },
738c2ecf20Sopenharmony_ci	.c0 = { 400, 398, 396, 384, 372, 354, 336, 312, 288, },
748c2ecf20Sopenharmony_ci};
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef5_m9 = {
778c2ecf20Sopenharmony_ci	.c2 = { -12, -18, -24, -28, -32, -38, -44, -46, 0, 2, 4, 2, 0, -2, -4, -8, },
788c2ecf20Sopenharmony_ci	.c1 = { 40, 68, 96, 128, 160, 196, 232, 268, -48, -46, -44, -36, -28, -14, 0, 20, },
798c2ecf20Sopenharmony_ci	.c0 = { 456, 450, 444, 428, 412, 388, 364, 334, 304, },
808c2ecf20Sopenharmony_ci};
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef5_m8 = {
838c2ecf20Sopenharmony_ci	.c2 = { 0, -4, -8, -16, -24, -32, -40, -48, 0, 2, 4, 6, 8, 6, 4, 2, },
848c2ecf20Sopenharmony_ci	.c1 = { 0, 28, 56, 94, 132, 176, 220, 266, -56, -60, -64, -62, -60, -50, -40, -20, },
858c2ecf20Sopenharmony_ci	.c0 = { 512, 506, 500, 478, 456, 424, 392, 352, 312, },
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef3_m32 = {
898c2ecf20Sopenharmony_ci	.c1 = { 108, 92, 76, 62, 48, 36, 24, 140, 256, 236, 216, 198, 180, 162, 144, 126, },
908c2ecf20Sopenharmony_ci	.c0 = { 296, 294, 292, 288, 284, 278, 272, 136, 256, },
918c2ecf20Sopenharmony_ci};
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef3_m26 = {
948c2ecf20Sopenharmony_ci	.c1 = { 104, 90, 76, 60, 44, 32, 20, 138, 256, 236, 216, 198, 180, 160, 140, 122, },
958c2ecf20Sopenharmony_ci	.c0 = { 304, 300, 296, 292, 288, 282, 276, 138, 256, },
968c2ecf20Sopenharmony_ci};
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef3_m22 = {
998c2ecf20Sopenharmony_ci	.c1 = { 100, 84, 68, 54, 40, 30, 20, 138, 256, 236, 216, 196, 176, 156, 136, 118, },
1008c2ecf20Sopenharmony_ci	.c0 = { 312, 310, 308, 302, 296, 286, 276, 138, 256, },
1018c2ecf20Sopenharmony_ci};
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef3_m19 = {
1048c2ecf20Sopenharmony_ci	.c1 = { 96, 80, 64, 50, 36, 26, 16, 136, 256, 236, 216, 194, 172, 152, 132, 114, },
1058c2ecf20Sopenharmony_ci	.c0 = { 320, 318, 316, 310, 304, 292, 280, 140, 256, },
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef3_m16 = {
1098c2ecf20Sopenharmony_ci	.c1 = { 88, 72, 56, 44, 32, 22, 12, 134, 256, 234, 212, 190, 168, 148, 128, 108, },
1108c2ecf20Sopenharmony_ci	.c0 = { 336, 332, 328, 320, 312, 300, 288, 144, 256, },
1118c2ecf20Sopenharmony_ci};
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef3_m14 = {
1148c2ecf20Sopenharmony_ci	.c1 = { 80, 64, 48, 36, 24, 16, 8, 132, 256, 232, 208, 186, 164, 142, 120, 100, },
1158c2ecf20Sopenharmony_ci	.c0 = { 352, 348, 344, 334, 324, 310, 296, 148, 256, },
1168c2ecf20Sopenharmony_ci};
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef3_m13 = {
1198c2ecf20Sopenharmony_ci	.c1 = { 72, 56, 40, 30, 20, 12, 4, 130, 256, 232, 208, 184, 160, 136, 112, 92, },
1208c2ecf20Sopenharmony_ci	.c0 = { 368, 364, 360, 346, 332, 316, 300, 150, 256, },
1218c2ecf20Sopenharmony_ci};
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef3_m12 = {
1248c2ecf20Sopenharmony_ci	.c1 = { 64, 50, 36, 26, 16, 10, 4, 130, 256, 230, 204, 178, 152, 128, 104, 84, },
1258c2ecf20Sopenharmony_ci	.c0 = { 384, 378, 372, 358, 344, 324, 304, 152, 256, },
1268c2ecf20Sopenharmony_ci};
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef3_m11 = {
1298c2ecf20Sopenharmony_ci	.c1 = { 56, 40, 24, 16, 8, 4, 0, 128, 256, 228, 200, 172, 144, 120, 96, 76, },
1308c2ecf20Sopenharmony_ci	.c0 = { 400, 396, 392, 376, 360, 336, 312, 156, 256, },
1318c2ecf20Sopenharmony_ci};
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef3_m10 = {
1348c2ecf20Sopenharmony_ci	.c1 = { 40, 26, 12, 6, 0, -2, -4, 126, 256, 226, 196, 166, 136, 110, 84, 62, },
1358c2ecf20Sopenharmony_ci	.c0 = { 432, 424, 416, 396, 376, 348, 320, 160, 256, },
1368c2ecf20Sopenharmony_ci};
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef3_m9 = {
1398c2ecf20Sopenharmony_ci	.c1 = { 24, 12, 0, -4, -8, -8, -8, 124, 256, 222, 188, 154, 120, 92, 64, 44, },
1408c2ecf20Sopenharmony_ci	.c0 = { 464, 456, 448, 424, 400, 366, 332, 166, 256, },
1418c2ecf20Sopenharmony_ci};
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic const struct tidss_scale_coefs coef3_m8 = {
1448c2ecf20Sopenharmony_ci	.c1 = { 0, -8, -16, -16, -16, -12, -8, 124, 256, 214, 172, 134, 96, 66, 36, 18, },
1458c2ecf20Sopenharmony_ci	.c0 = { 512, 502, 492, 462, 432, 390, 348, 174, 256, },
1468c2ecf20Sopenharmony_ci};
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ciconst struct tidss_scale_coefs *tidss_get_scale_coefs(struct device *dev,
1498c2ecf20Sopenharmony_ci						      u32 firinc,
1508c2ecf20Sopenharmony_ci						      bool five_taps)
1518c2ecf20Sopenharmony_ci{
1528c2ecf20Sopenharmony_ci	int i;
1538c2ecf20Sopenharmony_ci	int inc;
1548c2ecf20Sopenharmony_ci	static const struct {
1558c2ecf20Sopenharmony_ci		int mmin;
1568c2ecf20Sopenharmony_ci		int mmax;
1578c2ecf20Sopenharmony_ci		const struct tidss_scale_coefs *coef3;
1588c2ecf20Sopenharmony_ci		const struct tidss_scale_coefs *coef5;
1598c2ecf20Sopenharmony_ci		const char *name;
1608c2ecf20Sopenharmony_ci	} coefs[] = {
1618c2ecf20Sopenharmony_ci		{ 27, 32, &coef3_m32, &coef5_m32, "M32" },
1628c2ecf20Sopenharmony_ci		{ 23, 26, &coef3_m26, &coef5_m26, "M26" },
1638c2ecf20Sopenharmony_ci		{ 20, 22, &coef3_m22, &coef5_m22, "M22" },
1648c2ecf20Sopenharmony_ci		{ 17, 19, &coef3_m19, &coef5_m19, "M19" },
1658c2ecf20Sopenharmony_ci		{ 15, 16, &coef3_m16, &coef5_m16, "M16" },
1668c2ecf20Sopenharmony_ci		{ 14, 14, &coef3_m14, &coef5_m14, "M14" },
1678c2ecf20Sopenharmony_ci		{ 13, 13, &coef3_m13, &coef5_m13, "M13" },
1688c2ecf20Sopenharmony_ci		{ 12, 12, &coef3_m12, &coef5_m12, "M12" },
1698c2ecf20Sopenharmony_ci		{ 11, 11, &coef3_m11, &coef5_m11, "M11" },
1708c2ecf20Sopenharmony_ci		{ 10, 10, &coef3_m10, &coef5_m10, "M10" },
1718c2ecf20Sopenharmony_ci		{  9,  9, &coef3_m9, &coef5_m9, "M9" },
1728c2ecf20Sopenharmony_ci		{  4,  8, &coef3_m8, &coef5_m8, "M8" },
1738c2ecf20Sopenharmony_ci		/*
1748c2ecf20Sopenharmony_ci		 * When upscaling more than two times, blockiness and outlines
1758c2ecf20Sopenharmony_ci		 * around the image are observed when M8 tables are used. M11,
1768c2ecf20Sopenharmony_ci		 * M16 and M19 tables are used to prevent this.
1778c2ecf20Sopenharmony_ci		 */
1788c2ecf20Sopenharmony_ci		{  3,  3, &coef3_m11, &coef5_m11, "M11" },
1798c2ecf20Sopenharmony_ci		{  2,  2, &coef3_m16, &coef5_m16, "M16" },
1808c2ecf20Sopenharmony_ci		{  0,  1, &coef3_m19, &coef5_m19, "M19" },
1818c2ecf20Sopenharmony_ci	};
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	/*
1848c2ecf20Sopenharmony_ci	 * inc is result of 0x200000 * in_size / out_size. This dividing
1858c2ecf20Sopenharmony_ci	 * by 0x40000 scales it down to 8 * in_size / out_size. After
1868c2ecf20Sopenharmony_ci	 * division the actual scaling factor is 8/inc.
1878c2ecf20Sopenharmony_ci	 */
1888c2ecf20Sopenharmony_ci	inc = firinc / 0x40000;
1898c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(coefs); ++i) {
1908c2ecf20Sopenharmony_ci		if (inc >= coefs[i].mmin && inc <= coefs[i].mmax) {
1918c2ecf20Sopenharmony_ci			if (five_taps)
1928c2ecf20Sopenharmony_ci				return coefs[i].coef5;
1938c2ecf20Sopenharmony_ci			else
1948c2ecf20Sopenharmony_ci				return coefs[i].coef3;
1958c2ecf20Sopenharmony_ci		}
1968c2ecf20Sopenharmony_ci	}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	dev_err(dev, "%s: Coefficients not found for firinc 0x%08x, inc %d\n",
1998c2ecf20Sopenharmony_ci		__func__, firinc, inc);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	return NULL;
2028c2ecf20Sopenharmony_ci}
203