1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * motor driver
4 *
5 * Copyright (C) 2020 Rockchip Electronics Co., Ltd.
6 *
7 */
8 //#define DEBUG
9 #include <linux/io.h>
10 #include <linux/of_gpio.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/device.h>
14 #include <linux/fb.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/of_irq.h>
19 #include <linux/platform_device.h>
20 #include <linux/wakelock.h>
21 #include <linux/hrtimer.h>
22 #include <linux/pwm.h>
23 #include <linux/delay.h>
24 #include <media/v4l2-subdev.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-device.h>
27 #include <linux/mutex.h>
28 #include <linux/version.h>
29 #include <linux/rk-camera-module.h>
30 #include <linux/completion.h>
31 #include <linux/rk_vcm_head.h>
32
33 #define DRIVER_VERSION KERNEL_VERSION(0, 0x01, 0x00)
34
35 #define DRIVER_NAME "hall-dc"
36
37 #define IRIS_MAX_LOG 100
38 #define IRIS_LOG_STEP 1
39
40 #define PWM_PERIOD_DEF 333333
41
42 struct motor_dev {
43 struct v4l2_subdev sd;
44 struct v4l2_ctrl_handler ctrl_handler;
45 struct pwm_device *pwm;
46 struct device *dev;
47 struct mutex mutex;
48 struct pwm_state pwm_state;
49 u32 module_index;
50 const char *module_facing;
51 };
52
motor_dev_parse_dt(struct motor_dev * motor)53 static int motor_dev_parse_dt(struct motor_dev *motor)
54 {
55 struct device_node *node = motor->dev->of_node;
56 int ret = 0;
57 int error = 0;
58
59 motor->pwm = devm_pwm_get(motor->dev, NULL);
60 if (IS_ERR(motor->pwm)) {
61 error = PTR_ERR(motor->pwm);
62 if (error != -EPROBE_DEFER)
63 dev_err(motor->dev, "Failed to request PWM device: %d\n", error);
64 return error;
65 }
66 if (motor->pwm && motor->pwm->args.period != 0) {
67 motor->pwm_state.period = motor->pwm->args.period;
68 motor->pwm_state.polarity = motor->pwm->args.polarity;
69 } else {
70 motor->pwm_state.period = PWM_PERIOD_DEF;
71 motor->pwm_state.polarity = 0;
72 }
73 ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
74 &motor->module_index);
75 ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
76 &motor->module_facing);
77 if (ret) {
78 dev_err(motor->dev,
79 "could not get module information!\n");
80 return -EINVAL;
81 }
82 return 0;
83 }
84
motor_s_ctrl(struct v4l2_ctrl * ctrl)85 static int motor_s_ctrl(struct v4l2_ctrl *ctrl)
86 {
87 int ret = 0;
88 struct motor_dev *motor = container_of(ctrl->handler,
89 struct motor_dev, ctrl_handler);
90
91 switch (ctrl->id) {
92 case V4L2_CID_IRIS_ABSOLUTE:
93 motor->pwm_state.enabled = true;
94 motor->pwm_state.duty_cycle =
95 div64_u64((u64)motor->pwm_state.period * ctrl->val, IRIS_MAX_LOG);
96 pwm_apply_state(motor->pwm, &motor->pwm_state);
97 dev_dbg(motor->dev, "iris, ctrl->val %d, pwm duty %lld, period %lld, polarity %d\n",
98 ctrl->val,
99 motor->pwm_state.duty_cycle,
100 motor->pwm_state.period,
101 motor->pwm_state.polarity);
102 break;
103 default:
104 dev_err(motor->dev, "not support cmd %d\n", ctrl->id);
105 break;
106 }
107 return ret;
108 }
109
motor_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)110 static long motor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
111 {
112 return 0;
113 }
114
115 static const struct v4l2_subdev_core_ops motor_core_ops = {
116 .ioctl = motor_ioctl,
117 };
118
119 static const struct v4l2_subdev_ops motor_subdev_ops = {
120 .core = &motor_core_ops,
121 };
122
123 static const struct v4l2_ctrl_ops motor_ctrl_ops = {
124 .s_ctrl = motor_s_ctrl,
125 };
126
motor_initialize_controls(struct motor_dev * motor)127 static int motor_initialize_controls(struct motor_dev *motor)
128 {
129 struct v4l2_ctrl_handler *handler;
130 int ret = 0;
131
132 handler = &motor->ctrl_handler;
133 ret = v4l2_ctrl_handler_init(handler, 1);
134 if (ret)
135 return ret;
136 handler->lock = &motor->mutex;
137 v4l2_ctrl_new_std(handler, &motor_ctrl_ops,
138 V4L2_CID_IRIS_ABSOLUTE, 0, IRIS_MAX_LOG, IRIS_LOG_STEP, 0);
139
140 if (handler->error) {
141 ret = handler->error;
142 dev_err(motor->dev,
143 "Failed to init controls(%d)\n", ret);
144 goto err_free_handler;
145 }
146
147 motor->sd.ctrl_handler = handler;
148 return ret;
149
150 err_free_handler:
151 v4l2_ctrl_handler_free(handler);
152
153 return ret;
154 }
155
motor_dev_probe(struct platform_device * pdev)156 static int motor_dev_probe(struct platform_device *pdev)
157 {
158 int ret = 0;
159 struct motor_dev *motor;
160 struct v4l2_subdev *sd;
161 char facing[2];
162
163 dev_info(&pdev->dev, "driver version: %02x.%02x.%02x",
164 DRIVER_VERSION >> 16,
165 (DRIVER_VERSION & 0xff00) >> 8,
166 DRIVER_VERSION & 0x00ff);
167 motor = devm_kzalloc(&pdev->dev, sizeof(*motor), GFP_KERNEL);
168 if (!motor)
169 return -ENOMEM;
170 motor->dev = &pdev->dev;
171 dev_set_name(motor->dev, "motor");
172 dev_set_drvdata(motor->dev, motor);
173 if (motor_dev_parse_dt(motor)) {
174 dev_err(motor->dev, "parse dt error\n");
175 return -EINVAL;
176 }
177 mutex_init(&motor->mutex);
178 v4l2_subdev_init(&motor->sd, &motor_subdev_ops);
179 motor->sd.owner = pdev->dev.driver->owner;
180 motor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
181 motor->sd.dev = &pdev->dev;
182 v4l2_set_subdevdata(&motor->sd, pdev);
183 platform_set_drvdata(pdev, &motor->sd);
184 motor_initialize_controls(motor);
185 if (ret)
186 goto err_free;
187 ret = media_entity_pads_init(&motor->sd.entity, 0, NULL);
188 if (ret < 0)
189 goto err_free;
190 sd = &motor->sd;
191 sd->entity.function = MEDIA_ENT_F_LENS;
192 sd->entity.flags = 2;
193
194 memset(facing, 0, sizeof(facing));
195 if (strcmp(motor->module_facing, "back") == 0)
196 facing[0] = 'b';
197 else
198 facing[0] = 'f';
199 snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s",
200 motor->module_index, facing,
201 DRIVER_NAME);
202 ret = v4l2_async_register_subdev(sd);
203 if (ret)
204 dev_err(&pdev->dev, "v4l2 async register subdev failed\n");
205
206 dev_info(motor->dev, "gpio motor driver probe success\n");
207 return 0;
208 err_free:
209 v4l2_ctrl_handler_free(&motor->ctrl_handler);
210 v4l2_device_unregister_subdev(&motor->sd);
211 media_entity_cleanup(&motor->sd.entity);
212 return ret;
213 }
214
motor_dev_remove(struct platform_device * pdev)215 static int motor_dev_remove(struct platform_device *pdev)
216 {
217 struct v4l2_subdev *sd = platform_get_drvdata(pdev);
218 struct motor_dev *motor;
219
220 if (sd)
221 motor = v4l2_get_subdevdata(sd);
222 else
223 return -ENODEV;
224 if (sd)
225 v4l2_device_unregister_subdev(sd);
226 v4l2_ctrl_handler_free(&motor->ctrl_handler);
227 media_entity_cleanup(&motor->sd.entity);
228 return 0;
229 }
230
231 #if defined(CONFIG_OF)
232 static const struct of_device_id motor_dev_of_match[] = {
233 { .compatible = "rockchip,hall-dc", },
234 {},
235 };
236 #endif
237
238 static struct platform_driver motor_dev_driver = {
239 .driver = {
240 .name = DRIVER_NAME,
241 .owner = THIS_MODULE,
242 .of_match_table = of_match_ptr(motor_dev_of_match),
243 },
244 .probe = motor_dev_probe,
245 .remove = motor_dev_remove,
246 };
247
248 module_platform_driver(motor_dev_driver);
249
250 MODULE_LICENSE("GPL");
251 MODULE_ALIAS("platform:motor");
252 MODULE_AUTHOR("ROCKCHIP");
253