1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
/*
* Copyright (C) 2012 Spreadtrum Communications Inc.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/miscdevice.h>
#include <linux/platform_device.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <video/sprd_scale_k.h>
#include <linux/kthread.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/vmalloc.h>
#include "img_scale.h"
#include "compat_img_scale.h"
#include "dcam_drv.h"
#ifndef CONFIG_64BIT
#include <soc/sprd/hardware.h>
#endif
#define SCALE_DEVICE_NAME "sprd_scale"
#define SCALE_TIMEOUT 5000/*ms*/
#define SCALE_MINOR MISC_DYNAMIC_MINOR
#define SC_COEFF_BUF_SIZE (24 << 10)
static void scale_k_irq(void *fd)
{
struct scale_k_file *scale_file = (struct scale_k_file *)fd;
if (!scale_file) {
printk("scale_k_irq error: hand is null");
return;
}
printk("sc done.\n");
up(&scale_file->scale_done_sem);
}
static void scale_k_file_init(struct scale_k_file *fd, struct scale_k_private *scale_private)
{
fd->scale_private = scale_private;
sema_init(&fd->scale_done_sem, 0);
fd->drv_private.scale_fd = (void*)fd;
fd->drv_private.path_info.coeff_addr = scale_private->coeff_addr;
spin_lock_init(&fd->drv_private.scale_drv_lock);
sema_init(&fd->drv_private.path_info.done_sem, 0);
}
static int scale_k_open(struct inode *node, struct file *file)
{
int ret = 0;
struct scale_k_file *fd = NULL;
struct scale_k_private *scale_private = NULL;
struct miscdevice *md = file->private_data;
if (!md) {
ret = -EFAULT;
printk("scale_k_open error: miscdevice is null \n");
goto exit;
}
scale_private = (struct scale_k_private *)md->this_device->platform_data;
if (!scale_private) {
ret = -EFAULT;
printk("scale_k_open error: scale_private is null \n");
goto exit;
}
fd = vzalloc(sizeof(*fd));
if (!fd) {
ret = -ENOMEM;
printk("scale_k_open error: alloc \n");
goto exit;
}
fd->dn = md->this_device->of_node;
scale_k_file_init(fd, scale_private);
file->private_data = fd;
SCALE_TRACE("scale_k_open fd=%p ret=%d\n", fd, ret);
exit:
return ret;
}
ssize_t scale_k_read(struct file *file, char __user *u_data, size_t cnt, loff_t *cnt_ret)
{
uint32_t rt_word[2];
(void)file; (void)cnt; (void)cnt_ret;
if (cnt < sizeof(uint32_t)) {
printk("scale_k_read error: wrong size of u_data: %ld \n", cnt);
return -1;
}
rt_word[0] = SCALE_LINE_BUF_LENGTH;
rt_word[1] = SCALE_SC_COEFF_MAX;
return copy_to_user(u_data, (void*)rt_word, (uint32_t)(2 * sizeof(uint32_t)));
}
static int scale_k_release(struct inode *node, struct file *file)
{
struct scale_k_file *fd = NULL;
struct scale_k_private *scale_private = NULL;
fd = file->private_data;
if (!fd) {
goto exit;
}
scale_private = fd->scale_private;
if (!scale_private) {
goto fd_free;
}
down(&scale_private->start_sem);
up(&scale_private->start_sem);
fd_free:
vfree(fd);
fd = NULL;
file->private_data = NULL;
exit:
SCALE_TRACE("scale_k_release\n");
return 0;
}
static void sprd_img_print_reg(void)
{
uint32_t* reg_buf = NULL;
uint32_t reg_buf_len = 0x400;
int ret;
uint32_t print_len = 0, print_cnt = 0;
reg_buf = (uint32_t*)vzalloc(reg_buf_len);
if (NULL == reg_buf)
return;
ret = dcam_read_registers(reg_buf, ®_buf_len);
if (ret) {
vfree(reg_buf);
return;
}
printk("dcam registers \n");
while (print_len < reg_buf_len) {
printk("offset 0x%03x : 0x%08x, 0x%08x, 0x%08x, 0x%08x \n",
print_len,
reg_buf[print_cnt],
reg_buf[print_cnt+1],
reg_buf[print_cnt+2],
reg_buf[print_cnt+3]);
print_cnt += 4;
print_len += 16;
}
udelay(1);
vfree(reg_buf);
return;
}
static long scale_k_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int ret = 0;
struct scale_k_private *scale_private;
struct scale_k_file *fd;
struct scale_frame_param_t frame_params;
struct scale_slice_param_t slice_params;
fd = file->private_data;
if (!fd) {
ret = - EFAULT;
printk("scale_k_ioctl error: fd null \n");
goto ioctl_exit;
}
scale_private = fd->scale_private;
if (!scale_private) {
ret = -EFAULT;
printk("scale_k_ioctl erro: scale private null \n");
goto ioctl_exit;
}
switch (cmd) {
case SCALE_IO_START:
down(&scale_private->start_sem);
ret = scale_k_module_en(fd->dn);
if (unlikely(ret)) {
printk("rot_k_ioctl erro: scale_module_en\n");
up(&scale_private->start_sem);
goto ioctl_exit;
}
ret = scale_k_isr_reg(scale_k_irq, &fd->drv_private);
if (unlikely(ret)) {
printk("rot_k_ioctl error: scale_k_isr_reg\n");
scale_k_module_dis(fd->dn);
up(&scale_private->start_sem);
goto ioctl_exit;
}
ret = copy_from_user(&frame_params, (struct scale_frame_param_t *)arg, sizeof(frame_params));
if (ret) {
printk("rot_k_ioctl error: get frame param info \n");
scale_k_module_dis(fd->dn);
up(&scale_private->start_sem);
goto ioctl_exit;
}
ret = scale_k_start(&frame_params, &fd->drv_private.path_info);
if (ret) {
printk("rot_k_ioctl error: frame start \n");
scale_k_module_dis(fd->dn);
up(&scale_private->start_sem);
goto ioctl_exit;
}
break;
case SCALE_IO_DONE:
ret = down_timeout(&fd->scale_done_sem, msecs_to_jiffies(SCALE_TIMEOUT));
if (ret) {
printk("scale_k_ioctl error: interruptible time out\n");
sprd_img_print_reg();
goto ioctl_out;
}
scale_k_stop();
scale_k_module_dis(fd->dn);
up(&scale_private->start_sem);
break;
case SCALE_IO_CONTINUE:
/*Caution: slice scale is not supported by current driver.Please do not use it*/
ret = copy_from_user(&slice_params, (struct scale_slice_param_t *)arg, sizeof(slice_params));
if (ret) {
printk("rot_k_ioctl error: get slice param info \n");
goto ioctl_exit;
}
ret = scale_k_continue(&slice_params, &fd->drv_private.path_info);
if (ret) {
printk("rot_k_ioctl error: continue \n");
}
break;
default:
break;
}
ioctl_exit:
return ret;
ioctl_out:
dcam_resize_end();
scale_k_stop();
scale_k_module_dis(fd->dn);
up(&scale_private->start_sem);
return ret;
}
static struct file_operations scale_fops = {
.owner = THIS_MODULE,
.open = scale_k_open,
.read = scale_k_read,
.unlocked_ioctl = scale_k_ioctl,
.compat_ioctl = compat_scale_k_ioctl,
.release = scale_k_release,
};
static struct miscdevice scale_dev = {
.minor = SCALE_MINOR,
.name = SCALE_DEVICE_NAME,
.fops = &scale_fops,
};
int scale_k_probe(struct platform_device *pdev)
{
int ret;
struct scale_k_private *scale_private;
scale_private = devm_kzalloc(&pdev->dev, sizeof(*scale_private), GFP_KERNEL);
if (!scale_private) {
return -ENOMEM;
}
scale_private->coeff_addr = (void *)vzalloc(SC_COEFF_BUF_SIZE);
if (!scale_private->coeff_addr) {
devm_kfree(&pdev->dev, scale_private);
return -ENOMEM;
}
sema_init(&scale_private->start_sem, 1);
platform_set_drvdata(pdev, scale_private);
ret = misc_register(&scale_dev);
if (ret) {
printk("scale_k_probe error: ret=%d\n", ret);
ret = -EACCES;
goto probe_out;
}
scale_dev.this_device->of_node = pdev->dev.of_node;
scale_dev.this_device->platform_data = (void *)scale_private;
goto exit;
probe_out:
vfree(scale_private->coeff_addr);
devm_kfree(&pdev->dev, scale_private);
platform_set_drvdata(pdev, NULL);
exit:
return 0;
}
static int scale_k_remove(struct platform_device *pdev)
{
struct scale_k_private *scale_private;
scale_private = platform_get_drvdata(pdev);
if (!scale_private)
goto remove_exit;
misc_deregister(&scale_dev);
if (scale_private->coeff_addr) {
vfree(scale_private->coeff_addr);
}
devm_kfree(&pdev->dev, scale_private);
platform_set_drvdata(pdev, NULL);
remove_exit:
return 0;
}
static const struct of_device_id of_match_table_scale[] = {
{ .compatible = "sprd,sprd_scale", },
{ },
};
static struct platform_driver scale_driver =
{
.probe = scale_k_probe,
.remove = scale_k_remove,
.driver = {
.owner = THIS_MODULE,
.name = SCALE_DEVICE_NAME,
.of_match_table = of_match_ptr(of_match_table_scale),
}
};
int __init scale_k_init(void)
{
if (platform_driver_register(&scale_driver) != 0) {
printk("platform scale device register Failed \n");
return -1;
}
return 0;
}
void scale_k_exit(void)
{
platform_driver_unregister(&scale_driver);
}
module_init(scale_k_init);
module_exit(scale_k_exit);
MODULE_DESCRIPTION("Sprd Scale Driver");
MODULE_LICENSE("GPL");
|