1 // SPDX-License-Identifier: GPL-2.0
5 static int get_next_ulong(char **str_p
, unsigned long *val
, char *sep
, int base
)
9 if (!str_p
|| !(*str_p
))
12 p_val
= strsep(str_p
, sep
);
17 return kstrtoul(p_val
, base
, val
);
20 int fbtft_gamma_parse_str(struct fbtft_par
*par
, u32
*curves
,
21 const char *str
, int size
)
23 char *str_p
, *curve_p
= NULL
;
25 unsigned long val
= 0;
27 int curve_counter
, value_counter
;
33 tmp
= kmemdup(str
, size
+ 1, GFP_KERNEL
);
37 /* replace optional separators */
51 if (curve_counter
== par
->gamma
.num_curves
) {
52 dev_err(par
->info
->device
, "Gamma: Too many curves\n");
56 curve_p
= strsep(&str_p
, "\n");
59 if (value_counter
== par
->gamma
.num_values
) {
60 dev_err(par
->info
->device
,
61 "Gamma: Too many values\n");
65 ret
= get_next_ulong(&curve_p
, &val
, " ", 16);
69 _count
= curve_counter
* par
->gamma
.num_values
+
74 if (value_counter
!= par
->gamma
.num_values
) {
75 dev_err(par
->info
->device
, "Gamma: Too few values\n");
81 if (curve_counter
!= par
->gamma
.num_curves
) {
82 dev_err(par
->info
->device
, "Gamma: Too few curves\n");
93 sprintf_gamma(struct fbtft_par
*par
, u32
*curves
, char *buf
)
98 mutex_lock(&par
->gamma
.lock
);
99 for (i
= 0; i
< par
->gamma
.num_curves
; i
++) {
100 for (j
= 0; j
< par
->gamma
.num_values
; j
++)
101 len
+= scnprintf(&buf
[len
], PAGE_SIZE
,
102 "%04x ", curves
[i
* par
->gamma
.num_values
+ j
]);
105 mutex_unlock(&par
->gamma
.lock
);
110 static ssize_t
store_gamma_curve(struct device
*device
,
111 struct device_attribute
*attr
,
112 const char *buf
, size_t count
)
114 struct fb_info
*fb_info
= dev_get_drvdata(device
);
115 struct fbtft_par
*par
= fb_info
->par
;
116 u32 tmp_curves
[FBTFT_GAMMA_MAX_VALUES_TOTAL
];
119 ret
= fbtft_gamma_parse_str(par
, tmp_curves
, buf
, count
);
123 ret
= par
->fbtftops
.set_gamma(par
, tmp_curves
);
127 mutex_lock(&par
->gamma
.lock
);
128 memcpy(par
->gamma
.curves
, tmp_curves
,
129 par
->gamma
.num_curves
* par
->gamma
.num_values
*
130 sizeof(tmp_curves
[0]));
131 mutex_unlock(&par
->gamma
.lock
);
136 static ssize_t
show_gamma_curve(struct device
*device
,
137 struct device_attribute
*attr
, char *buf
)
139 struct fb_info
*fb_info
= dev_get_drvdata(device
);
140 struct fbtft_par
*par
= fb_info
->par
;
142 return sprintf_gamma(par
, par
->gamma
.curves
, buf
);
145 static struct device_attribute gamma_device_attrs
[] = {
146 __ATTR(gamma
, 0660, show_gamma_curve
, store_gamma_curve
),
149 void fbtft_expand_debug_value(unsigned long *debug
)
151 switch (*debug
& 0x7) {
153 *debug
|= DEBUG_LEVEL_1
;
156 *debug
|= DEBUG_LEVEL_2
;
159 *debug
|= DEBUG_LEVEL_3
;
162 *debug
|= DEBUG_LEVEL_4
;
165 *debug
|= DEBUG_LEVEL_5
;
168 *debug
|= DEBUG_LEVEL_6
;
176 static ssize_t
store_debug(struct device
*device
,
177 struct device_attribute
*attr
,
178 const char *buf
, size_t count
)
180 struct fb_info
*fb_info
= dev_get_drvdata(device
);
181 struct fbtft_par
*par
= fb_info
->par
;
184 ret
= kstrtoul(buf
, 10, &par
->debug
);
187 fbtft_expand_debug_value(&par
->debug
);
192 static ssize_t
show_debug(struct device
*device
,
193 struct device_attribute
*attr
, char *buf
)
195 struct fb_info
*fb_info
= dev_get_drvdata(device
);
196 struct fbtft_par
*par
= fb_info
->par
;
198 return sysfs_emit(buf
, "%lu\n", par
->debug
);
201 static struct device_attribute debug_device_attr
=
202 __ATTR(debug
, 0660, show_debug
, store_debug
);
204 void fbtft_sysfs_init(struct fbtft_par
*par
)
206 device_create_file(par
->info
->dev
, &debug_device_attr
);
207 if (par
->gamma
.curves
&& par
->fbtftops
.set_gamma
)
208 device_create_file(par
->info
->dev
, &gamma_device_attrs
[0]);
211 void fbtft_sysfs_exit(struct fbtft_par
*par
)
213 device_remove_file(par
->info
->dev
, &debug_device_attr
);
214 if (par
->gamma
.curves
&& par
->fbtftops
.set_gamma
)
215 device_remove_file(par
->info
->dev
, &gamma_device_attrs
[0]);