hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// SPDX-License-Identifier: GPL-2.0
/*
 * Rockchip driver for IR-Cuter
 *
 * Copyright (C) 2020 Fuzhou Rockchip Electronics Co., Ltd.
 *
 * V0.0X01.0X00 first version.
 */
#include <linux/io.h>
#include <linux/of_gpio.h>
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/completion.h>
#include <linux/mutex.h>
#include <linux/workqueue.h>
#include <linux/platform_device.h>
#include <media/v4l2-subdev.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <linux/fs.h>
#include <linux/version.h>
#include <linux/rk-camera-module.h>
 
#define RK_IRCUT_NAME "ircut"
#define DRIVER_VERSION    KERNEL_VERSION(0, 0x01, 0x00)
 
//#define USED_SYS_DEBUG
 
enum IRCUT_STATE_e {
   IRCUT_STATE_CLOSED,
   IRCUT_STATE_CLOSING,
   IRCUT_STATE_OPENING,
   IRCUT_STATE_OPENED,
};
 
struct ircut_op_work {
   struct work_struct work;
   int op_cmd;
   struct ircut_dev *dev;
};
 
struct ircut_drv_data {
   int    (*parse_dt)(struct ircut_dev *ircut, struct device_node *node);
   void    (*ctrl)(struct ircut_dev *ircut, int cmd);
};
 
struct ircut_dev {
   struct v4l2_subdev sd;
   struct v4l2_ctrl_handler ctrl_handler;
   struct device *dev;
   struct completion complete;
   /* state mutex */
   struct mutex mut_state;
   enum IRCUT_STATE_e state;
   struct workqueue_struct *wq;
   int pulse_width;
   int val;
   struct gpio_desc *open_gpio;
   struct gpio_desc *close_gpio;
   struct gpio_desc *led_gpio;
   u32 module_index;
   const char *module_facing;
   const struct ircut_drv_data *drv_data;
};
 
#define IRCUT_STATE_EQ(expected) \
   ((ircut->state == (expected)) ? true : false)
 
static int ap1511a_parse_dt(struct ircut_dev *ircut, struct device_node *node)
{
   ircut->open_gpio = devm_gpiod_get(ircut->dev, "ircut-open", GPIOD_OUT_HIGH);
   if (IS_ERR(ircut->open_gpio)) {
       dev_err(ircut->dev, "Failed to get ircut-open-gpios\n");
       return PTR_ERR(ircut->open_gpio);
   }
 
   ircut->led_gpio = devm_gpiod_get_optional(ircut->dev, "led", GPIOD_OUT_LOW);
   if (IS_ERR(ircut->led_gpio))
       dev_err(ircut->dev, "Failed to get led-gpios\n");
 
   return 0;
}
 
static void ap1511a_ctrl(struct ircut_dev *ircut, int cmd)
{
   if (cmd > 0) {
       gpiod_set_value_cansleep(ircut->open_gpio, 1);
       if (!IS_ERR(ircut->led_gpio))
           gpiod_set_value_cansleep(ircut->led_gpio, 0);
   } else {
       gpiod_set_value_cansleep(ircut->open_gpio, 0);
       if (!IS_ERR(ircut->led_gpio))
           gpiod_set_value_cansleep(ircut->led_gpio, 1);
   }
}
 
static int ba6208_parse_dt(struct ircut_dev *ircut, struct device_node *node)
{
   int ret;
 
   dev_dbg(ircut->dev, "ircut_gpio_parse_dt");
   ret = of_property_read_u32(node,
       "rockchip,pulse-width",
       &ircut->pulse_width);
   if (ret != 0) {
       ircut->pulse_width = 100;
       dev_err(ircut->dev,
           "failed get pulse-width,use dafult value 100\n");
   }
   if (ircut->pulse_width > 2000) {
       ircut->pulse_width = 300;
       dev_info(ircut->dev,
           "pulse width to long,use default dafult 300");
   }
   dev_dbg(ircut->dev, "pulse-width value from dts %d\n",
       ircut->pulse_width);
   /* get ircut open gpio */
   ircut->open_gpio = devm_gpiod_get(ircut->dev, "ircut-open", GPIOD_OUT_LOW);
   if (IS_ERR(ircut->open_gpio))
       dev_err(ircut->dev, "Failed to get ircut-open-gpios\n");
 
   /* get ircut close gpio */
   ircut->close_gpio = devm_gpiod_get(ircut->dev,
               "ircut-close", GPIOD_OUT_LOW);
   if (IS_ERR(ircut->close_gpio))
       dev_err(ircut->dev, "Failed to get ircut-close-gpios\n");
 
   /* get led gpio */
   ircut->led_gpio = devm_gpiod_get_optional(ircut->dev, "led", GPIOD_OUT_LOW);
   if (IS_ERR(ircut->led_gpio))
       dev_err(ircut->dev, "Failed to get led-gpios\n");
 
   return 0;
}
 
static void ba6208_ctrl(struct ircut_dev *ircut, int cmd)
{
   if (cmd > 0) {
       if (!IS_ERR(ircut->open_gpio))
           gpiod_set_value_cansleep(ircut->open_gpio, 1);
       msleep(ircut->pulse_width);
       if (!IS_ERR(ircut->open_gpio))
           gpiod_set_value_cansleep(ircut->open_gpio, 0);
       if (!IS_ERR(ircut->led_gpio))
           gpiod_set_value_cansleep(ircut->led_gpio, 0);
   } else {
       if (!IS_ERR(ircut->close_gpio))
           gpiod_set_value_cansleep(ircut->close_gpio, 1);
       msleep(ircut->pulse_width);
       if (!IS_ERR(ircut->close_gpio))
           gpiod_set_value_cansleep(ircut->close_gpio, 0);
       if (!IS_ERR(ircut->led_gpio))
           gpiod_set_value_cansleep(ircut->led_gpio, 1);
   }
}
 
static void ircut_op_work(struct work_struct *work)
{
   struct ircut_op_work *wk =
       container_of(work, struct ircut_op_work, work);
   struct ircut_dev *ircut = wk->dev;
   enum IRCUT_STATE_e state;
 
   if (ircut->drv_data->ctrl)
       ircut->drv_data->ctrl(ircut, wk->op_cmd);
 
   state = (wk->op_cmd > 0) ? IRCUT_STATE_OPENED : IRCUT_STATE_CLOSED;
   mutex_lock(&ircut->mut_state);
   complete(&ircut->complete);
   ircut->state = state;
   mutex_unlock(&ircut->mut_state);
   kfree(wk);
   wk = NULL;
}
 
static int ircut_operation(struct ircut_dev *ircut, int op)
{
   struct ircut_op_work *wk = NULL;
   bool should_wait = false;
   bool do_nothing = false;
   enum IRCUT_STATE_e old_state;
 
   dev_dbg(ircut->dev, "%s, status %d\n", __func__, op);
   mutex_lock(&ircut->mut_state);
   old_state = ircut->state;
   if (op > 0) {
       /* check state */
       if (IRCUT_STATE_EQ(IRCUT_STATE_OPENING) ||
           IRCUT_STATE_EQ(IRCUT_STATE_OPENED)) {
           /* already in opening or opened state, do nothing */
           do_nothing = true;
       } else if (IRCUT_STATE_EQ(IRCUT_STATE_CLOSING)) {
           /* in closing state,should wait */
           should_wait = true;
       } else {
           /* in closed state,queue work */
       }
   } else {
       /* check state */
       if (IRCUT_STATE_EQ(IRCUT_STATE_CLOSING) ||
           IRCUT_STATE_EQ(IRCUT_STATE_CLOSED)) {
           /* already in closing or closed state, do nothing */
           do_nothing = true;
       } else if (IRCUT_STATE_EQ(IRCUT_STATE_OPENING)) {
           /* in opening state,should wait */
           should_wait = true;
       } else {
           /* in opened state,queue work */
       }
   }
   mutex_unlock(&ircut->mut_state);
   if (do_nothing)
       goto op_done;
   if (should_wait)
       wait_for_completion(&ircut->complete);
   wk = kmalloc(sizeof(*wk),
       GFP_KERNEL);
   if (!wk) {
       dev_err(ircut->dev, "failed to alloc ircut work struct\n");
       goto err_ircut_operation;
   }
   wk->op_cmd = op;
   wk->dev = ircut;
   mutex_lock(&ircut->mut_state);
   if (op > 0)
       ircut->state = IRCUT_STATE_OPENING;
   else
       ircut->state = IRCUT_STATE_CLOSING;
   reinit_completion(&ircut->complete);
   mutex_unlock(&ircut->mut_state);
   /* queue work */
   INIT_WORK(&wk->work, ircut_op_work);
   if (!queue_work(ircut->wq, &wk->work)) {
       dev_err(ircut->dev, "queue work failed\n");
       kfree(wk);
       ircut->state = old_state;
       goto err_ircut_operation;
   }
op_done:
   return 0;
err_ircut_operation:
   return -1;
}
 
#ifdef USED_SYS_DEBUG
static ssize_t set_ircut_status(struct device *dev,
   struct device_attribute *attr,
   const char *buf,
   size_t count)
{
   struct ircut_dev *ircut = dev_get_drvdata(dev);
   int status = 0;
   int ret = 0;
 
   ret = sscanf(buf, "%d", &status);
   if (ret)
       ircut_operation(ircut, status);
   return count;
}
 
static struct device_attribute attributes[] = {
   __ATTR(sys_set_ircut, S_IWUSR, NULL, set_ircut_status),
};
 
static int add_sysfs_interfaces(struct device *dev)
{
   int i;
 
   for (i = 0; i < ARRAY_SIZE(attributes); i++)
       if (device_create_file(dev, attributes + i))
           goto undo;
   return 0;
undo:
   for (i--; i >= 0 ; i--)
       device_remove_file(dev, attributes + i);
   dev_err(dev, "%s: failed to create sysfs interface\n", __func__);
   return -ENODEV;
}
#endif
 
static int ircut_s_ctrl(struct v4l2_ctrl *ctrl)
{
   int ret = -EINVAL;
   struct ircut_dev *ircut = container_of(ctrl->handler,
                        struct ircut_dev, ctrl_handler);
 
   if (ctrl->id == V4L2_CID_BAND_STOP_FILTER) {
       ret = ircut_operation(ircut, ctrl->val);
       if (ret == 0)
           ircut->val = ctrl->val;
   }
   return ret;
}
 
static long ircut_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
{
   return -EINVAL;
}
 
static const struct v4l2_subdev_core_ops ircut_core_ops = {
   .ioctl = ircut_ioctl,
};
 
static const struct v4l2_subdev_ops ircut_subdev_ops = {
   .core    = &ircut_core_ops,
};
 
static const struct v4l2_ctrl_ops ircut_ctrl_ops = {
   .s_ctrl = ircut_s_ctrl,
};
 
static const struct ircut_drv_data ap1511a_drv_data = {
   .parse_dt    = ap1511a_parse_dt,
   .ctrl        = ap1511a_ctrl,
};
 
static const struct ircut_drv_data ba6208_drv_data = {
   .parse_dt    = ba6208_parse_dt,
   .ctrl        = ba6208_ctrl,
};
 
#if defined(CONFIG_OF)
static const struct of_device_id ircut_of_match[] = {
   { .compatible = "rockchip,ircut",
       .data = &ba6208_drv_data },
   { .compatible = "ap1511a,ircut",
       .data = &ap1511a_drv_data },
   {},
};
MODULE_DEVICE_TABLE(of, ircut_of_match);
#endif
 
static int ircut_probe(struct platform_device *pdev)
{
   struct ircut_dev *ircut = NULL;
   struct device_node *node = pdev->dev.of_node;
   struct v4l2_ctrl_handler *handler;
   const struct of_device_id *match;
   int ret = 0;
   struct v4l2_subdev *sd;
   char facing[2];
 
   dev_info(&pdev->dev, "driver version: %02x.%02x.%02x",
       DRIVER_VERSION >> 16,
       (DRIVER_VERSION & 0xff00) >> 8,
       DRIVER_VERSION & 0x00ff);
   ircut = devm_kzalloc(&pdev->dev, sizeof(*ircut), GFP_KERNEL);
   if (!ircut) {
       dev_err(&pdev->dev, "alloc ircut failed\n");
       return -ENOMEM;
   }
 
   match = of_match_node(ircut_of_match, pdev->dev.of_node);
   if (!match)
       return -ENODEV;
 
   ircut->drv_data = match->data;
 
   ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
                  &ircut->module_index);
   ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
                      &ircut->module_facing);
   if (ret) {
       dev_err(&pdev->dev,
           "could not get module information!\n");
       return -EINVAL;
   }
   ircut->dev = &pdev->dev;
   mutex_init(&ircut->mut_state);
   init_completion(&ircut->complete);
   ircut->wq = alloc_workqueue("ircut wq",
           WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
   v4l2_subdev_init(&ircut->sd, &ircut_subdev_ops);
   ircut->sd.owner = pdev->dev.driver->owner;
   ircut->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
   ircut->sd.dev = &pdev->dev;
   v4l2_set_subdevdata(&ircut->sd, pdev);
   platform_set_drvdata(pdev, &ircut->sd);
 
   handler = &ircut->ctrl_handler;
   ret = v4l2_ctrl_handler_init(handler, 1);
   if (ret)
       return ret;
   v4l2_ctrl_new_std(handler, &ircut_ctrl_ops,
       V4L2_CID_BAND_STOP_FILTER, 0, 4, 1, 1);
   if (handler->error) {
       ret = handler->error;
       dev_err(&pdev->dev, "Failed to init controls(%d)\n", ret);
       goto err_free;
   }
   ircut->sd.ctrl_handler = handler;
   ret = media_entity_pads_init(&ircut->sd.entity, 0, NULL);
   if (ret < 0)
       goto err_free;
 
   if (ircut->drv_data->parse_dt) {
       ret = ircut->drv_data->parse_dt(ircut, node);
       if (ret)
           goto err_free;
   }
 
   sd = &ircut->sd;
   sd->entity.function = MEDIA_ENT_F_LENS;
   sd->entity.flags = 1;
 
   memset(facing, 0, sizeof(facing));
   if (strcmp(ircut->module_facing, "back") == 0)
       facing[0] = 'b';
   else
       facing[0] = 'f';
 
   snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s",
        ircut->module_index, facing,
        RK_IRCUT_NAME);
   ret = v4l2_async_register_subdev(sd);
   if (ret)
       dev_err(&pdev->dev, "v4l2 async register subdev failed\n");
 
   /* set default state to open */
   ircut_operation(ircut, 3);
   ircut->val = 3;
#ifdef USED_SYS_DEBUG
   add_sysfs_interfaces(ircut->dev);
#endif
   dev_info(&pdev->dev, "probe successful!");
   return 0;
 
err_free:
   v4l2_ctrl_handler_free(handler);
   v4l2_device_unregister_subdev(&ircut->sd);
   media_entity_cleanup(&ircut->sd.entity);
   return ret;
}
 
static int ircut_drv_remove(struct platform_device *pdev)
{
   struct v4l2_subdev *sd = platform_get_drvdata(pdev);
   struct ircut_dev *ircut = NULL;
 
   if (sd)
       ircut = v4l2_get_subdevdata(sd);
   if (ircut && ircut->wq)
       drain_workqueue(ircut->wq);
   if (sd)
       v4l2_device_unregister_subdev(sd);
   v4l2_ctrl_handler_free(&ircut->ctrl_handler);
   media_entity_cleanup(&ircut->sd.entity);
   return 0;
}
 
static struct platform_driver ircut_driver = {
   .driver = {
       .name = RK_IRCUT_NAME,
       .of_match_table = of_match_ptr(ircut_of_match),
   },
   .probe = ircut_probe,
   .remove = ircut_drv_remove,
};
 
module_platform_driver(ircut_driver);
 
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:rockchip-ircut");
MODULE_AUTHOR("ROCKCHIP");