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
;
29 fbtft_par_dbg(DEBUG_SYSFS
, par
, "%s() str=\n", __func__
);
34 fbtft_par_dbg(DEBUG_SYSFS
, par
, "%s\n", str
);
36 tmp
= kmemdup(str
, size
+ 1, GFP_KERNEL
);
40 /* replace optional separators */
54 if (curve_counter
== par
->gamma
.num_curves
) {
55 dev_err(par
->info
->device
, "Gamma: Too many curves\n");
59 curve_p
= strsep(&str_p
, "\n");
62 if (value_counter
== par
->gamma
.num_values
) {
63 dev_err(par
->info
->device
,
64 "Gamma: Too many values\n");
68 ret
= get_next_ulong(&curve_p
, &val
, " ", 16);
71 curves
[curve_counter
* par
->gamma
.num_values
+ value_counter
] = val
;
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
* sizeof(tmp_curves
[0]));
130 mutex_unlock(&par
->gamma
.lock
);
135 static ssize_t
show_gamma_curve(struct device
*device
,
136 struct device_attribute
*attr
, char *buf
)
138 struct fb_info
*fb_info
= dev_get_drvdata(device
);
139 struct fbtft_par
*par
= fb_info
->par
;
141 return sprintf_gamma(par
, par
->gamma
.curves
, buf
);
144 static struct device_attribute gamma_device_attrs
[] = {
145 __ATTR(gamma
, 0660, show_gamma_curve
, store_gamma_curve
),
148 void fbtft_expand_debug_value(unsigned long *debug
)
150 switch (*debug
& 0x7) {
152 *debug
|= DEBUG_LEVEL_1
;
155 *debug
|= DEBUG_LEVEL_2
;
158 *debug
|= DEBUG_LEVEL_3
;
161 *debug
|= DEBUG_LEVEL_4
;
164 *debug
|= DEBUG_LEVEL_5
;
167 *debug
|= DEBUG_LEVEL_6
;
175 static ssize_t
store_debug(struct device
*device
,
176 struct device_attribute
*attr
,
177 const char *buf
, size_t count
)
179 struct fb_info
*fb_info
= dev_get_drvdata(device
);
180 struct fbtft_par
*par
= fb_info
->par
;
183 ret
= kstrtoul(buf
, 10, &par
->debug
);
186 fbtft_expand_debug_value(&par
->debug
);
191 static ssize_t
show_debug(struct device
*device
,
192 struct device_attribute
*attr
, char *buf
)
194 struct fb_info
*fb_info
= dev_get_drvdata(device
);
195 struct fbtft_par
*par
= fb_info
->par
;
197 return snprintf(buf
, PAGE_SIZE
, "%lu\n", par
->debug
);
200 static struct device_attribute debug_device_attr
=
201 __ATTR(debug
, 0660, show_debug
, store_debug
);
203 void fbtft_sysfs_init(struct fbtft_par
*par
)
205 device_create_file(par
->info
->dev
, &debug_device_attr
);
206 if (par
->gamma
.curves
&& par
->fbtftops
.set_gamma
)
207 device_create_file(par
->info
->dev
, &gamma_device_attrs
[0]);
210 void fbtft_sysfs_exit(struct fbtft_par
*par
)
212 device_remove_file(par
->info
->dev
, &debug_device_attr
);
213 if (par
->gamma
.curves
&& par
->fbtftops
.set_gamma
)
214 device_remove_file(par
->info
->dev
, &gamma_device_attrs
[0]);